user1927396
user1927396

Reputation: 459

os.path.isdir check failing

I have the following code where I am trying to check for directory "Gerrits/HEAD/wlan" and then do some operations,for some reason if condition to check for the directory keeps failing even thought the directory exists? anything wrong with the if condition@if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): below

import os
import subprocess
from subprocess import check_call

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0]
print SCRIPT_ROOT

def main ():
    if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")):
        print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip"
        check_call("rm -rf $SCRIPT_ROOT/Gerrits/HEAD/wlan ", shell=True)
        check_call("cd Gerrits/HEAD",shell=True)
    else:
        print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it"
        os.makedirs("Gerrits/HEAD/wlan")
        check_call("cd Gerrits/HEAD",shell=True)
        currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0]

if __name__ == '__main__':
    main()

Error:-

SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    main()
  File "test.py", line 16, in main
    os.makedirs("Gerrits/HEAD/wlan")
  File "/usr/lib/python2.6/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: 'Gerrits/HEAD/wlan'

Upvotes: 0

Views: 818

Answers (2)

bgporter
bgporter

Reputation: 36574

When I execute this line of code here:

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0]

...the value of SCRIPT_ROOT has a trailing newline

>>> import os
>>> import subprocess
>>> ROOT = subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0]
>>> ROOT
'/Users/bgporter/personal\n'

...which makes this call

if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")):

behave differently than you'd like it to. You can either call strip() on that value, or if you always want to get the current working directory, you can do that much more easily by calling os.getcwd()

Similarly, you can use the os.removedirs() function to recursively remove the directories you don't want instead of shelling out.

Upvotes: 1

favoretti
favoretti

Reputation: 30207

Add .strip() to your communicate()[0] calls, the code as is includes trailing newline in the output.

Just to be sure, your script that I just tested on a linux box with Python 2.5.

import os
import subprocess
from subprocess import check_call

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].strip()
print SCRIPT_ROOT

def main ():
    if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")):
        print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip"
        check_call("rm -rf %s/Gerrits/HEAD/wlan" % SCRIPT_ROOT, shell=True)
        check_call("cd Gerrits/HEAD",shell=True)
    else:
        print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it"
        os.makedirs("Gerrits/HEAD/wlan")
        check_call("cd Gerrits/HEAD",shell=True)
        currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].strip()

if __name__ == '__main__':
    main()

And its output:

vlazarenko@xx:~$ python o.py 
/media/home/vlazarenko
SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip

Upvotes: 1

Related Questions