I’m trying to change the name of my csv file with python. So I know that when I want a filename it gives gives a path for instance
C:/user/desktop/somefolder/[someword].csv
so what I want to do is to change this file name to something like
C:/user/desktop/somefolder/[someword] [somenumber].csv
but I don’t know what that number is automatically, or the word is automatically the word was generated from another code I don’t have access to, the number is generated from the python code I have. so I just want to change the file name to include the [someword] and the [somenumber] before the .csv
I have the os library for python installed incase that’s a good library to use for that.
>Solution :
Here is the solution (no extra libs needed):
import os
somenumber = 1 # use number generated in your code
file_path = "/Users/hevlich/comeet/comeet-server-3.0/fake"
for full_filename in os.listdir(file_path):
# `someword` is a file name without an extension in that context
someword, file_extension = os.path.splitext(full_filename)
os.rename(f"{file_path}/{full_filename}", f"{file_path}/{someword} {somenumber}{file_extension}")