【发布时间】:2020-01-31 18:54:20
【问题描述】:
我想要一个 python 脚本来完成这三个任务:
- 检查路径是否包含复制到特定目标的word文件
- 检查路径是否包含复制到特定目标的pdf文件
- 检查路径是否包含目录并将洞文件夹复制到 具体目的地。
出于这个原因,我使用 os.walk() 列出路径的目录和文件 我正在使用 shutil library 来复制文件和目录。
代码
import os
from distutils.dir_util import copy_tree
import shutil
from os import path
import datetime
def main():
src = "C:/Users/LT GM/Desktop/Python_files/"
dst2 = "C:/Users/LT GM/Desktop/"
for root,dirs,files in os.walk(src):
for name in files:
print("files: ",os.path.join(root,name))
for name in dirs:
copieddst = copy_tree(src,dst2)
print("directory: ",os.path.join(root,name))
print(" coppied directory :{0}".format(copieddst) )
# make a duplicate of an existing file
if path.exists(src):
# get the path to the file in the current directory
print("****")
src = path.realpath("pandas.pdf")
#seperate the path from the filter
head, tail = path.split(src)
print("path:" +head)
print("file:" +tail)
dst =str(datetime.date.today()) + tail
# nowuse the shell to make a copy of the file
shutil.copy(src, dst)
if __name__=="__main__":
main()
问题是我可以复制文件或目录的内容。不是hole目录以及如何检查它的pdf或doc文件?
【问题讨论】: