[英]How do I rename a file based on string?
我有以下代碼來重命名具有硬編碼文件名的文件。 當然日期可以更改。
import shutil
# Source path
source = "C:\\WeeklyReports\\temp\\we mmddyy_DONE.xlsx"
# Destination path
destination = "C:\\WeeklyReports\\we 101219.xlsx"
# Copy the content of
# source to destination
shutil.copyfile(source, destination)
如何用字符串 format_mmddyy 替換源文件名(我們 mmddyy_DONE.xlsx)中的 mmddyy,如下所示?
format_mmddyy
'100619'
type(format_mmddyy)
str
您可以使用str.replace
function
import shutil
# Source path
source = "C:\\WeeklyReports\\temp\\we mmddyy_DONE.xlsx"
format_mmddyy = 100619
destination = source.replace('mmddyy_DONE', format_mmddyy)
shutil.copyfile(source, destination)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.