Home > Mobile >  Renaming csv file with a variable name and moving to destination folder
Renaming csv file with a variable name and moving to destination folder

Time:01-25

I have a csv file in downloads folder with a name export.csv. I need to rename the file in the format "REMSIMPORT today's date" and move to destination folder. For e.g. today's date is 2022-01-24. So export.csv will be renamed as REMSIMPORT20220124. Tomorrow it will be REMSIMPORT20220125. So it will change every time as per current date

This is how far I have reached.

from datetime import date

src_path = r'C:/Users/abc/Downloads/'
dest_path = r"C:\Users\abc\Desktop\Weekly"

today_date = date.today()  # Extracted today's date
today_date = str(today_date).replace("-","")

I am confused on how to rename file with a variable newname (e.g. REMSIMPORT20220124) and moving to destination folder.

CodePudding user response:

Use the rename method from os module.

import os

src_path = r'C:/Users/abc/Downloads/'   'REMSIMPORT'
dest_path = r'C:/Users/abc/Desktop/Weekly/'   'REMSIMPORT'   today_date
os.rename(src_path, dest_path)

CodePudding user response:

You may use schedule for your purpose.

$ pip install schedule

Here is some examples of schedule from the page :

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

On your purpose, you can try something like this :

from datetime import date
import pandas as pd
import schedule
import time

def save_csv():
    today_date = str(date.today()).replace("-","")
    src_path = r'C:/Users/abc/Downloads/'
    dest_path = r'C:\Users\abc\Desktop\Weekly'
    df = pd.read_csv(src_path   'export.csv')
    df.to_csv(dest_path   'REMSIMPORT'   today_date   '.csv')

schedule.every().day.at("10:30").do(save_csv)

while True:
    schedule.run_pending()
    time.sleep(1)

CodePudding user response:

Shutil copyfile should be good!

from datetime import date
from shutil import copyfile

src_path = r'C:/Users/user/adifferentdir'
dest_path = r"C:/Users/user/dir"

today_date = date.today()  # Extracted today's date
today_date = str(today_date).replace("-","")

copyfile(src_path "/export.csv",dest_path "/REMSIMPORT" today_date ".csv")#copying file with new name```

CodePudding user response:

I think using the shutil.move() utility function would be the best option because not only can it move files, it can rename them at the same time. I've also found that using the pathlib module makes file and directory path manipulations not only extremely easy but at very intuitive (and allow doing so in an OS portable way).

Anyway, here's an example of putting them to use to accomplish what you want to do.

from datetime import date
from pathlib import Path
from shutil import move

filename = 'export.csv'
#src_dir = Path('C:/Users/abc/Downloads/')
#dst_dir = Path('C:/Users/abc/Desktop/Weekly')

# Directories for testing.
src_dir = Path('./').resolve()
dst_dir = Path('./Weekly').resolve()

prefix = 'REMSIMPORT'
today_date = date.today()  # Extracted today's date
today_date = str(today_date).replace('-', '')
newname = prefix   today_date

if not dst_dir.exists():
    dst_dir.mkdir()  # Create destination directory since it doesn't exist.

src_path = src_dir / filename
dst_path = dst_dir / (newname   src_path.suffix)
move(src_path, dst_path)

print('fini')
  •  Tags:  
  • Related