如何复制已创建的文件

问题描述 投票:0回答:1

1-以下程序的第一个功能运行良好,但是我不知道如何在第二个功能中调用第一个功能并将其结果复制到新目录中。

2-如何在第一个功能的不同行中打印已建立的文件? (每个文件都在一行中)

谢谢。

# 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 founded files into another directory
def founded_files(dst):
    dst = os.chdir('D:\\secondfolder')
    for files in find_all():
        shutil.copytree(dst)


founded_files(dst)
python function copy shutil
1个回答
0
投票

您缺少功能参数。它应该像

def founded_files(dst):
    dst = os.chdir('D:\\secondfolder')
    for files in find_all('*.jpg', 'D:\\firstfolder'):
        shutil.copytree(dst)
© www.soinside.com 2019 - 2024. All rights reserved.