Python Forum

Full Version: How to rename a CSV file by adding MODIFIED in the filename?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hello all,

I am working and editing a CSV file using python. In my python code I want to add the following:

I have a CSV file "Book1.csv" for example. After I have run my python code and modification have been made (added column for example), the file should be placed in the same folder and directory and have the name: "Book1_MODIFIED.csv".

How can this been done in the Python language?

Like to hear.
Overwriting original file is dangerous.
Better to write a new file, and purge older ones.
At any rate, show your code so far.
I am sorry for the late response. Here is what I got so far:

import os
from pathlib import Path
import pandas as pd

df = pd.read_csv('Book1.csv')

df["NEW COLUMN"] = ""


df.to_csv('MODIFIED_NAME_OF_THE_ORIGIN_FILE.csv', index = False)
Is there anyone who can help me with this query?

Thanks in advance.
replace in 'MODIFIED_NAME_OF_THE_ORIGIN_FILE.csv' with your file name, 'Book1_MODIFIED.csv' line 10
(Nov-29-2020, 04:07 PM)Larz60+ Wrote: [ -> ]replace in 'MODIFIED_NAME_OF_THE_ORIGIN_FILE.csv' with your file name, 'Book1_MODIFIED.csv' line 10

I prefer having a code which 'looks' to the file name and insert it in line 10. In this case, I should always make the code specific for each csv file...
This is basic python. How do you think this would be done?
I don't know, that's the reason I try to find out on this forum.

Should this work in a 'for' loop? Is that what you mean?
I'm not sure what you are looking for.
I'm guessing that you want to pass the filenames into a function,
and then open the file.
Is that correct?
I am sorry for the confusion, I think my description has not been clear.

I am seeking for a function which checks the current csv file on its filename. This filename should be remembered in the code and after adjustments, the filename should be used with an addition.

For example:
I have a csv file (Book1.csv) which I want to analyse. After the analysis, the file should be saved with an addition 'MODIFIED' behind the current file name. The code should execute something which 'looks' to the current filename and use it with an addition of MODIFIED. --> Book1_MODIFIED.csv

Hopefully this makes some more sense.

Like to hear.
Pages: 1 2 3