1

I have this script:

import os, sys, shutil, glob
from distutils.dir_util import copy_tree

if not os.path.exists('Files'):
    os.makedirs('Files')

source_dir = os.path.join(os.environ["HOMEPATH"], "Desktop")
dest_dir = 'Files'

copy_tree(source_dir, dest_dir)

But I want to copy only a specific file (.txt), how can I do it?

Have a good day.

LuCash
  • 23
  • 4
  • If you want to copy a specific file from one directory to another, is there any reason you can't use [shutil.copy()](https://docs.python.org/3/library/shutil.html#shutil.copy) – luthervespers Sep 14 '20 at 21:55
  • because I can't copy the files in sub folders, I want to copy ALL specific files in a directory – LuCash Sep 15 '20 at 00:07
  • If I understand correctly, you would like to copy all files (which may be nested in subdirectories) to a single folder? EDIT: all files with a certain name? – luthervespers Sep 17 '20 at 00:13
  • yes, all files with a certain name including sub-folders. For example I put the Desktop path, I want it to take into consideration the files in the Desktop and the files in folders – LuCash Sep 18 '20 at 17:53
  • You can use the 'ignore' keyword and flip it)to include only files of a certain name. See this post: https://stackoverflow.com/questions/42487578/python-shutil-copytree-use-ignore-function-to-keep-specific-files-types This will copy all folders to match the nesting of the source directory, however it will also copy folders when no match is found. – luthervespers Sep 24 '20 at 01:10

0 Answers0