簡體   English   中英

用變量名重命名 csv 文件並移動到目標文件夾

[英]Renaming csv file with a variable name and moving to destination folder

我在下載文件夾中有一個名為export.csv的 csv 文件。 我需要以“REMSIMPORT + 今天的日期”格式重命名文件並移動到目標文件夾。 例如,今天的日期是 2022-01-24。 所以export.csv將被重命名為REMSIMPORT20220124 明天將是REMSIMPORT20220125 所以它會根據當前日期每次更改

這就是我已經達到的程度。

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("-","")

我對如何使用變量newname (例如REMSIMPORT20220124 )重命名文件並移動到目標文件夾感到困惑。

使用os module中的rename方法。

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)

您可以根據您的目的使用schedule

$ pip install schedule

以下是頁面中的一些schedule示例:

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)

根據您的目的,您可以嘗試這樣的事情:

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)

Shutil copyfile 應該不錯!

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```

我認為使用shutil.move()實用程序 function 將是最好的選擇,因為它不僅可以移動文件,還可以同時重命名它們。 我還發現使用pathlib模塊使文件和目錄路徑操作不僅非常簡單而且非常直觀(並且允許以操作系統可移植的方式進行操作)。

無論如何,這是一個使用它們來完成您想做的事情的示例。

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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM