Python os.makedirs to recreate path

17,195

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
17,195
Visceral
Author by

Visceral

I am a GIS student in the process of completing my practicum for a mining company located in the heart of downtown Vancouver.

Updated on June 04, 2022

Comments

  • Visceral
    Visceral almost 2 years

    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