script.py:
#!/usr/bin/python
import os
srcDir = os.getcwd()
dirName = 'target_directory'
dstDir = os.path.abspath(dirName)
def ignore_list(path, files):
filesToIgnore = []
for fileName in files:
fullFileName = os.path.join(os.path.normpath(path), fileName)
if (not os.path.isdir(fullFileName)
and not fileName.endswith('pyc')
and not fileName.endswith('ui')
and not fileName.endswith('txt')
and not fileName == '__main__.py'
and not fileName == 'dcpp.bat'):
filesToIgnore.append(fileName)
return filesToIgnore
# start of script
shutil.copytree(srcDir, dstDir, ignore=ignore_list)
As shutil.copytree()
has no option where I can give names for required files to copy like "ignore," I have modified the argument of ignore to give "required files to copy."
Review my code.
1 Answer 1
Looks ok for a specific task, but the code is not reusable at all. It would be more useful to create a function ignore_except
that could be used like this to perform the same task:
shutil.copytree(srcDir, dstDir,
ignore=ignore_except('*.pyc', '*.ui', '*.txt', '__main__.py', 'dcpp.bat'))
The source code of shutil.ignore_patterns
would be a good starting point for such function.
-
\$\begingroup\$ pls paste here code of ignore_except() \$\endgroup\$Patrick– Patrick2014年03月02日 13:19:59 +00:00Commented Mar 2, 2014 at 13:19
-
\$\begingroup\$ @Patrick: See the
include_patterns()
function in my answer to a related Python question on stackoverflow titled Copying specific files to a new folder, while maintaining the original subdirectory tree. \$\endgroup\$martineau– martineau2018年12月08日 22:11:26 +00:00Commented Dec 8, 2018 at 22:11