4
\$\begingroup\$

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.

Calak
2,39111 silver badges19 bronze badges
asked Feb 19, 2014 at 9:27
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

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.

martineau
2751 silver badge10 bronze badges
answered Feb 27, 2014 at 19:22
\$\endgroup\$
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.