【发布时间】:2020-05-27 15:28:21
【问题描述】:
我有一个脚本,可以将文件夹和文件从它们的源复制到新的目的地,该脚本使用shutil 模块可以正常工作。但我正在硬编码文件夹的源代码。
我需要让脚本选择具有特定名称的所需文件夹。 as
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 库)。如果需要,则使用 dst=r'string' 可能会更好。