Python Forum
How to rename a CSV file by adding MODIFIED in the filename?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to rename a CSV file by adding MODIFIED in the filename?
#1
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.
Reply
#2
Overwriting original file is dangerous.
Better to write a new file, and purge older ones.
At any rate, show your code so far.
Reply
#3
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)
Reply
#4
Is there anyone who can help me with this query?

Thanks in advance.
Reply
#5
replace in 'MODIFIED_NAME_OF_THE_ORIGIN_FILE.csv' with your file name, 'Book1_MODIFIED.csv' line 10
Reply
#6
(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...
Reply
#7
This is basic python. How do you think this would be done?
Reply
#8
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?
Reply
#9
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?
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rename first row in a CSV file James_S 3 589 Dec-17-2023, 05:20 AM
Last Post: James_S
  PDF properties doesn't show created or modified date Pedroski55 4 1,099 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  rename file RolanRoll 0 535 May-18-2023, 02:17 PM
Last Post: RolanRoll
  File path by adding various variables Mishal0488 2 1,041 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  '' FTP '' File upload with a specified string and rename midomarc 1 1,170 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  output provide the filename along with the input file processed. arjunaram 1 944 Apr-13-2023, 08:15 PM
Last Post: menator01
  rename same file names in different directories elnk 0 718 Nov-04-2022, 05:23 PM
Last Post: elnk
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,248 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Rename part of filename in multiple files atomxkai 7 7,375 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Adding to an XML file TeXaSpEtE83 0 1,276 Dec-22-2021, 08:28 AM
Last Post: TeXaSpEtE83

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020