Как скопировать найденные файлы - PullRequest
0 голосов
/ 21 октября 2019

Первая функция следующей программы работает хорошо, но

  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 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)

1 Ответ

0 голосов
/ 21 октября 2019

Вам не хватает параметров функции. Это должно быть похоже на

def founded_files(dst):
    dst = os.chdir('D:\\secondfolder')
    for files in find_all('*.jpg', 'D:\\firstfolder'):
        shutil.copytree(dst)
...