我有一个脚本,它可以将文件夹和文件从源文件复制到新的目标文件,该脚本使用shutil
模块正常工作。但是我正在硬编码文件夹的源代码
我需要的是让脚本选择它具有特定名称的所需文件夹。作为
pdf 11-12-02-2020所以它是pdf+日期昨天-当前日期-当前月份-当前年份
我该怎么做
代码:
import os
import shutil
from os import path
import datetime
src = "I:\\"
src2 = "I:/pdf 11-12-02-2020"
dst = "C:/Users/LT GM/Desktop/pdf 11-12-02-2020/"
def main():
copy()
def copy():
if os.path.exists(dst):
shutil.rmtree(dst)
print("the deleted folder is :{0}".format(dst))
#copy the folder as it is (folder with the files)
copieddst = shutil.copytree(src2,dst)
print("copy of the folder is done :{0}".format(copieddst))
else:
#copy the folder as it is (folder with the files)
copieddst = shutil.copytree(src2,dst)
print("copy of the folder is done :{0}".format(copieddst))
if __name__=="__main__":
main()
您正在寻找^{} 模块
此外,您可能希望使用
os
模块为您正确解析路径,请参见this,因为src
变量似乎未使用,所以最好将其删除,同时牢记以下几点:作为旁注,在本例中
dst = os.path.join(os.path.expanduser("~"), "Desktop" ,name)
很有效,实际上不建议使用它,请参见我的答案here相关问题 更多 >
编程相关推荐