【发布时间】:2019-10-21 05:51:47
【问题描述】:
下面程序的第一个函数效果很好,但是
不知道如何在第二个函数中调用第一个并将其结果复制到新目录中。
如何在不同的行中打印第一个函数中找到的文件? (每个文件一行)
谢谢。
# Write a program that walks through a folder tree and searches for files with
# a certain file extension (such as .pdf or .jpg). Copy these files from whatever
# location they are in to a new folder.
import os, shutil, fnmatch
def find_all(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
print(find_all('*.jpg', 'D:\\firstfolder'))
# Copy the found files into another directory
def found_files(dst):
dst = os.chdir('D:\\secondfolder')
for files in find_all():
shutil.copytree(dst)
found_files(dst)
【问题讨论】:
标签: python function copy shutil