3

I want to go through every line in a text file of existing paths and file names, divide the strings into drive, path, and filename. Then what I would like to do is copy the files with their paths to a new location - either a different drive or append to an existing file tree (i.e., if S:\A\B\C\D\E\F.shp is the original file. I wish to append it to the new location as C:\users\visc\A\B\C\D\E\F.shp

Due to my poor programming skills, I continue to receive the error:

File "C:\Users\visc\a\b.py", line 28, in <module> (destination) = os.makedirs( pathname, 0755 ); 

Here is my code:

import os,sys, shutil

## Open the file with read only permit f = open('C:/Users/visc/a/b/c.txt') destination = ('C:/Users/visc') # read line by line for line in f: line = line.replace("\\\\", "\\") #split the drive and path using os.path.splitdrive (drive, pathname) = os.path.splitdrive(line) #split the path and fliename using os.path.split (pathname, filename) = os.path.split(pathname) #print the stripped line print line.strip() #print the drive, path, and filename info print('Drive is %s Path is %s and file is %s' % (drive, pathname, filename)) (destination) = os.makedirs( pathname, 0755 ); print "Path is Created" 

Thank you

2
  • 4
    You didn't include the actual error message. Commented May 10, 2012 at 18:10
  • Sorry, here is my full error message: Traceback (most recent call last): File "C:\Users\visc\a\b.py", line 28, in <module> (destination) = os.makedirs( pathname, 0755 ); File "C:\Python26\ArcGIS10.0\lib\os.py", line 157, in makedirs mkdir(name, mode) WindowsError: [Error 183] Cannot create a file when that file already exists: '\\A\\B\\C\\D\\E' Commented May 10, 2012 at 18:31

3 Answers 3

5

What you need to do is either check for the existence of the folder before calling makedirs() or handle the exception that occurs if the folder already exists. In Python it's a little more conventional to handle the exception, so change your makedirs() line:

try: (destination) = os.makedirs( pathname, 0755 ) except OSError: print "Skipping creation of %s because it exists already."%pathname 

The strategy of checking for the folder before trying to create it is known as "Look Before You Leap" or LBYL; the strategy of handling expected errors is "Easier to Ask Forgiveness than Permission" or EAFP. The advantage to EAFP is that it correctly handles the case where the folder is created by another process between the check and the makedirs() call.

Sign up to request clarification or add additional context in comments.

2 Comments

The error occurs because he's constructing the directories in the wrong location - catching the exception will only mask the problem.
I generally shy away from masking ALL exceptions of a particular type. It would be much better to do something like this except OSError, e: if e.errno != errno.EEXIST: raise
3

I guess you want something like

os.makedirs(os.path.join(destination, pathname), 0755 ) 

if you want to connect the file paths given by pathname with the new destination given by destination. Your code currently tries to create a file in the same place as before (at least it looks like this - can't say for sure since I don't know what's in the file you're reading and what is your current directory).

If you assign the result of the call to os.makedirs() to destination (the parentheses are as useless as the semicolon in that line), you effectively set destination to None since os.makedirs() doesn't actually return anything. And you're not using it to construct your new path.

1 Comment

Sorry, here is my full error message: ` File "C:\Users\visc\a\b.py", line 28, in <module> (destination) = os.makedirs( pathname, 0755 ); File "C:\Python26\ArcGIS10.0\lib\os.py", line 157, in makedirs mkdir(name, mode) WindowsError: [Error 183] Cannot create a file when that file already exists: '\\A\\B\\C\\D\\E'`
2

Python 3.2 added an exist_ok optional parameter:

os.makedirs(name, mode=0o777, exist_ok=False)

If you have the luxury of being allowed to use Python 3, this might a better (and safer) option.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.