Shutil.copy and Glob
I am trying to copy all the files containing 'BNALP' to another directory called 'source'...I tried using the glob and shutil function to do this but an error message always arises stating "TypeError: coer开发者_如何学Pythoncing to Unicode: need string or buffer, list found". I was wondering if anyone could help me in the right direction because I am new to python.
Have you tried the solutions provided on How do I copy a file in python? Based on half-remembered python and your error message, are you trying to copy a list of files to a destination? If so, you'd need to iterate through them calling the copy each time.
See also
- copy multiple files in python
- Python Windows File Copy with Wildcard Support
- Python: How to move a file with unicode filename to a unicode folder
- How do I use wild cards in python as specified in a text file
To iterate through an enumerable object in python, you would want to use "in" Rough code lifted from unicode link above
destination = '/etc/tmp/source'
# magic here loads the list of BNALP files into a list variable
# could be something like
# files = os.listdir('/etc/BNALP')
for file in files:
shutil.copy2(file, destination)
精彩评论