copy text files to multiple directories using python

3659
8
07-27-2013 12:28 AM
debclifford
New Contributor
I am trying to copy text files to various directories that are listed in a text file but keep getting errors, can anyone assist please ?

Python script below: and I have attached my text file


import os
import shutil
import string

textfile = (r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt')

with open(r'C:\ArcReader_Servers.txt', 'rb') as fp:
        for line in fp.readlines():
            filepath = line
            print filepath
            for i in textfile:
                filename = i
                print filename
                shutil.copy2(filename,filepath)

error I receive when I run is :

C:\\Offences\\data


C:\Offences\state_jul.txt
Traceback (most recent call last):
  File "C:\Offences\Distribution_Offences_ArcReader.py", line 14, in <module>
    shutil.copy2(filename,filepath)
  File "C:\Python26\ArcGIS10.0\lib\shutil.py", line 99, in copy2
    copyfile(src, dst)
  File "C:\Python26\ArcGIS10.0\lib\shutil.py", line 53, in copyfile
    fdst = open(dst, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\\\Offences\\\\data\r\n'
Tags (2)
0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus
if this line
textfile = (r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt')

is supposed to be a list, then it must be enclosed in square brackets
textfile = [r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt']
0 Kudos
debclifford
New Contributor
if this line
textfile = (r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt')

is supposed to be a list, then it must be enclosed in square brackets
textfile = [r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt']


Tried that change but still get the same error ?
0 Kudos
T__WayneWhitley
Frequent Contributor
Not 100% certain but it appears it almost works, just you have a problem with the line return characters in retrieving the filename to copy from the text file:

IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\\\Offences\\\\data\r\n'

...think you need to strip \r\n (which is line return\feed), easy enough to do, see the following test example entered interactively from IDLE - the print statement shows what you got on the return (which is 2 lines) and the 2nd print statement shows the corrected result (which is 1 line) after using strip:
>>> textfile = 'C:\\\\Offences\\\\data\r\n'
>>> print "'" + textfile + "'"
'C:\\Offences\\data

'
>>> textfile = textfile.strip()
>>> print "'" + textfile + "'"
'C:\\Offences\\data'
>>> 


This is just an oft overlooked minor detail to take care of in reading text files.

Enjoy,
Wayne
0 Kudos
Luke_Pinner
MVP Regular Contributor
spud007, Wayne is correct, have a read of the filename in the error message ( filename: 'C:\\\\Offences\\\\data\r\n').  Also, next time you post code, please use [noparse]
[/noparse] tags, read this if you're not sure how.

if this line
textfile = (r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt')

is supposed to be a list, then it must be enclosed in square brackets
textfile = [r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt']


No, parentheses are fine. 

(r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt') is a tuple which is a sequence type similar to lists, but is immutable (can't be modified once created).
0 Kudos
debclifford
New Contributor
Not 100% certain but it appears it almost works, just you have a problem with the line return characters in retrieving the filename to copy from the text file:

IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\\\Offences\\\\data\r\n'

...think you need to strip \r\n (which is line return\feed), easy enough to do, see the following test example entered interactively from IDLE - the print statement shows what you got on the return (which is 2 lines) and the 2nd print statement shows the corrected result (which is 1 line) after using strip:
>>> textfile = 'C:\\\\Offences\\\\data\r\n'
>>> print "'" + textfile + "'"
'C:\\Offences\\data

'
>>> textfile = textfile.strip()
>>> print "'" + textfile + "'"
'C:\\Offences\\data'
>>> 


This is just an oft overlooked minor detail to take care of in reading text files.

Enjoy,
Wayne


Thanks Wayne for your help...I am just learning and am still a little confused by what you have suggested and where I make the changes !!
The idea of the script was to read the first file listed in the textfile = statement then open the text file called ArcReader_Servers.txt which consists of directory paths and copy the file to each of the locations. I only included the print statements as I was trying to see what was going wrong. I then want it to go to the 2nd file in the textfile = statement and repeat the process.
Sorry for my lack of understanding but am still learning and appreciate the help.
Thanks
0 Kudos
Luke_Pinner
MVP Regular Contributor
import os
import shutil
import string

textfile = (r'C:\Offences\state_jul.txt',r'C:\Offences\state_jun.txt')

with open(r'C:\ArcReader_Servers.txt', 'r') as fp:
    for filepath in fp.readlines():
        filepath=filepath.strip() #Strip off the \r\n
        print filepath
        for filename in textfile:
            print filename
            shutil.copy2(filename,filepath)
0 Kudos
debclifford
New Contributor
Thankyou so much for all your help, it worked perfectly and I have learnt so much. Really appreciate the help
0 Kudos
Luke_Pinner
MVP Regular Contributor
Then you should click the check mark/tick next to Wayne's answer to indicate that it answered your question.
0 Kudos