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?
#26
Just to clarify:

In the original code (posted below again), parts was used to create the output file name, and had nothing to do with input file name (as requested by member). There are several ways to do this, I chose splitting into parts, adding in 'Modified' to end of filename, and joining back together.

Original code:
import csv
from pathlib import Path
from tkinter.filedialog import askopenfilename
import sys
 
 
def read_csv_file(filename):
    parts = list(filename.parts)
    parts[-1] = f"{filename.stem}Modified{filename.suffix}"
    parts[0] = ''
    outfilename = Path(f"{'/'.join(parts)}")
 
    print(f"new output file name: {outfilename}")
 
    with filename.open() as fp, outfilename.open('w') as fout:
        crdr = csv.reader(fp, delimiter=',')
        cwrtr = csv.writer(fout, delimiter=',')
        for row in crdr:
            print(row)
            # Modify row as desired here
            cwrtr.writerow(row)
 
def get_filename():
    badcount = 0
 
    while(True):
        try:
            if badcount > 2:
                print("Three strikes and your out!")
                sys.exit(-1)
            filename = Path(askopenfilename(filetypes=[("CSV files","*.csv")]))
            break
        except TypeError:
            badcount += 1
            print(f"bad filename, try again")
         
    return filename
 
 
if __name__ == '__main__':
    read_csv_file(get_filename())
Please note f-strings are not being properly displayed. items within brackets should be highlighted as they are placeholders for the variable they contain.
Reply


Messages In This Thread
RE: How to rename a CSV file by adding MODIFIED in the filename? - by Larz60+ - Dec-13-2020, 12:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  rename same file names in different directories elnk 5 2,417 Jul-12-2024, 01:43 PM
Last Post: snippsat
  Extract and rename a file from an Archive tester_V 4 3,734 Jul-08-2024, 07:54 AM
Last Post: tester_V
  Rename first row in a CSV file James_S 3 1,609 Dec-17-2023, 05:20 AM
Last Post: James_S
  PDF properties doesn't show created or modified date Pedroski55 4 2,628 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  rename file RolanRoll 0 1,082 May-18-2023, 02:17 PM
Last Post: RolanRoll
  File path by adding various variables Mishal0488 2 3,904 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  '' FTP '' File upload with a specified string and rename midomarc 1 2,226 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  output provide the filename along with the input file processed. arjunaram 1 1,506 Apr-13-2023, 08:15 PM
Last Post: menator01
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,972 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Rename part of filename in multiple files atomxkai 7 10,765 Feb-18-2022, 10:03 PM
Last Post: atomxkai

Forum Jump:

User Panel Messages

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