yesraaj
yesraaj

Reputation: 47900

How to read path from a txt file and copy those file to a new directory?

from shutil import  copy
f = open(r'C:\temp.txt', 'r')
for i in f.readlines():
    print i
    copy(i,r"C:\opencascade")    

f.close()

I am reading path from temp.txt file which has 500 lines each line is a path for specific file to be copied to location "C:\opencascade" How to convert 'i' in above code to be raw string to make the code work

Error from interpreter

    copy(i,r"C:\opencascade")    
  File "C:\Python26\lib\shutil.py", line 88, in copy
    copyfile(src, dst)
  File "C:\Python26\lib\shutil.py", line 52, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\OPENCASCADE6.2.0\\ROS\\INC\\toptools_hsequenceofshape.hxx\n'

Upvotes: 2

Views: 6232

Answers (2)

jumpjack
jumpjack

Reputation: 1

Error is single "\" rather than double "\" in the path.

Upvotes: 0

wr.
wr.

Reputation: 2859

You have a \n at the and of the filename.

Try:

copy(i.strip(), r"C:\opencascade")

Upvotes: 8

Related Questions