I have the following code to rename a file with hardcoded file names. Of course the date can change.
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)
How do I replace mmddyy in the source filename (we mmddyy_DONE.xlsx) with string format_mmddyy as shown below?
format_mmddyy
'100619'
type(format_mmddyy)
str
You can use the 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.