Desperado
Desperado

Reputation: 125

os.path.exists is not working as expected in python

I am trying to create a directory in the home path and re-check if the directory exists in the home path before re-creating using os.path.exists(), but its not working as expected.

if os.access("./", os.W_OK) is not True:
    print("Folder not writable")
    dir_name_tmp = subprocess.Popen('pwd', stdout=subprocess.PIPE, shell=True)
    dir_name_tmp = dir_name_tmp.stdout.read()
    dir_name = dir_name_tmp.split('/')[-1]
    dir_name = dir_name.rstrip()

    os.system('ls ~/')
    print "%s"%dir_name

    if not os.path.exists("~/%s"%(dir_name)):
        print "Going to create a new folder %s in home path\n"%(dir_name)
        os.system('mkdir ~/%s'%(dir_name))
    else:
        print "Folder %s Already Exists\n"%(dir_name)
        os.system('rm -rf ~/%s & mkdir ~/%s'%(dir_name, dir_name))
else :
    print("Folder writable")

Output for the first time:

Folder not writable
Desktop  Downloads  Perforce  bkp  doc  project
hello.list
Going to create a new folder hello.list in home path

Output for the 2nd time:

Folder not writable
Desktop  Downloads  Perforce  bkp  doc  hello.list  project
hello.list
Going to create a new folder hello.list in home path

mkdir: cannot create directory `/home/desperado/hello.list': File exists

Its not going into the else loop though the directory is existing. Am I missing something ? Share in you inputs !

Updated Working Code With Suggestions Provided: Using $HOME directory and os.path.expandusr

if os.access("./", os.W_OK) is not True:
    log.debug("Folder Is Not writable")
    dir_name_tmp = subprocess.Popen('pwd', stdout=subprocess.PIPE, shell=True)
    dir_name_tmp = dir_name_tmp.stdout.read()
    dir_name = dir_name_tmp.split('/')[-1]
    dir_name = dir_name.rstrip()

    log.debug("dir_name is %s"%dir_name)

    dir_name_path = (os.path.expanduser('~/%s'%(dir_name))).rstrip()
    log.debug("dir_name_path is %s"%(dir_name_path))

    # if not os.path.exists('~/%s'%(dir_name)):
    if not os.path.exists('%s'%(dir_name_path)):
        log.debug("Going to create a new folder %s in home path\n"%(dir_name))
        os.system('mkdir $HOME/%s'%(dir_name))
    else:
        log.debug("Folder %s Already Exists\n"%(dir_name))
        os.system('rm -rf %s'%(dir_name_path))
        os.system('mkdir $HOME/%s'%(dir_name))
else :
    log.debug("Folder Is Writable")

Upvotes: 2

Views: 2740

Answers (2)

Box Box Box Box
Box Box Box Box

Reputation: 5240

As mentioned by VPfB, the tilde symbol is understood literally by Python. To fix this, you need to get your actual home directory.

Now, on different platforms, there are different paths for the home directory.

To get the home directory, os.path.expanduser will be useful.

>>> import os
>>> os.path.expanduser("~")
'/Users/ashish'

Upvotes: 1

VPfB
VPfB

Reputation: 17267

The tilde symbol ~ representing the home directory is a shell convention. It is expanded by the shell in os.system, but it is understood literally in Python.

So you create <HOME>/<DIR>, but test for ~/<DIR>.

Upvotes: 6

Related Questions