Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Edit CSV filename in Python to append to the current filename

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading