![](/img/trans.png)
[英]How to extract string from a filename containing date using regex in python?
[英]xcopy command gives an error with filename containing variable with 'date' string
我在嘗試使用 python 腳本中的 xcopy 命令復制文件時收到錯誤“參數數量無效”。 我有一個變量“date_str”作為文件名的一部分,因為這是每天執行的 SQL 數據庫的備份。 請幫助我,這是我的腳本:
import os
import time
import pandas as pd
date_str = pd.Timestamp.today().strftime('%d-%m-%Y')
try:
os.system('cmd /k "xcopy", SiaBKP_{date_str}_.sql D:/Dumps/mysql/compressed')
except:
print("Could not execute command.")
好的,在使用@wkl 解決方案玩了一下之后,我終於弄明白了。 目標文件夾的路徑需要雙“\”才能正常工作,而且整個命令字符串需要單引號,因此最終腳本如下:
import os
import time
import pandas as pd
date_str = pd.Timestamp.today().strftime('%d-%m-%Y')
try:
os.system(rf'xcopy SiafBKP_{date_str}_.sql D:\\Dumps\\mysql\\compressed')
except:
print("Could not execute command.")
像魅力一樣工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.