如何使用 Python select 将特定目录从其源复制到新目标?
How to select a specific directory to copy from its source to new destination using Python?
我有一个脚本可以将文件夹和文件从它们的源复制到一个新的目的地,该脚本使用 shutil
模块可以正常工作。但我正在硬编码文件夹的源代码。
我需要的是使脚本 select 成为所需的文件夹,并为其指定名称。作为
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()
您正在寻找 datetime
模块。
此外,您可能希望使用 os
模块为您正确解析路径,请参阅 this,因为 src
变量似乎未被使用,最好将其删除,考虑到所有这些:
import calendar
import os
import shutil
from datetime import date
from os import path
def yesterday():
day = int(date.today().strftime("%d"))
month = int(date.today().strftime("%m"))
year = int(date.today().strftime("%Y"))
if day != 1:
return day - 1
long_months = [1, 3, 5, 7, 8, 10, 12]
if month in long_months:
return 31
elif month == 2:
if calendar.isleap(year):
return 29
return 28
else:
return 30
name = "pdf " + str(yesterday()) + date.today().strftime("-%d-%m-%Y")
src2 = os.path.join("I:/", name)
dst = os.path.join(os.path.expanduser("~"), "Desktop",name)
作为旁注,而在这种情况下
dst = os.path.join(os.path.expanduser("~"), "Desktop" ,name)
有效,其实不建议使用,看我的回答
我有一个脚本可以将文件夹和文件从它们的源复制到一个新的目的地,该脚本使用 shutil
模块可以正常工作。但我正在硬编码文件夹的源代码。
我需要的是使脚本 select 成为所需的文件夹,并为其指定名称。作为
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()
您正在寻找 datetime
模块。
此外,您可能希望使用 os
模块为您正确解析路径,请参阅 this,因为 src
变量似乎未被使用,最好将其删除,考虑到所有这些:
import calendar
import os
import shutil
from datetime import date
from os import path
def yesterday():
day = int(date.today().strftime("%d"))
month = int(date.today().strftime("%m"))
year = int(date.today().strftime("%Y"))
if day != 1:
return day - 1
long_months = [1, 3, 5, 7, 8, 10, 12]
if month in long_months:
return 31
elif month == 2:
if calendar.isleap(year):
return 29
return 28
else:
return 30
name = "pdf " + str(yesterday()) + date.today().strftime("-%d-%m-%Y")
src2 = os.path.join("I:/", name)
dst = os.path.join(os.path.expanduser("~"), "Desktop",name)
作为旁注,而在这种情况下
dst = os.path.join(os.path.expanduser("~"), "Desktop" ,name)
有效,其实不建议使用,看我的回答