Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.isdir returns false when folder exists?

Tags:

python

os.path

I have the following code which checks if the directory exists

def download(id, name, bar):
    cwd = os.getcwd()
    dir = os.path.join(cwd,bar)
    partial = os.path.join(cwd, id + ".partial")
    print os.path.isdir(dir)
    if(os.path.isdir(dir)):
        print "dir exists"
        dir_match_file(dir, bar)
    else:
        print dir

For a directory that actually exists, it returns "False". Here is the output:

False
/scratch/rists/djiao/Pancancer/RNA-seq/CESC/TCGA-BI-A0VS-01A-11R-A10U-07

When I go to python interactive session and type in os.path.isdir("/scratch/rists/djiao/Pancancer/RNA-seq/CESC/TCGA-BI-A0VS-01A-11R-A10U-07"), it returns "true".

Why does it say false when the folder exists?

like image 239
ddd Avatar asked Jul 25 '14 21:07

ddd


People also ask

What does OS path Isdir return?

path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows symbolic link, that means if the specified path is a symbolic link pointing to a directory then the method will return True.

What is OS path Isdir dir?

The os. path. isdir() is a built-in Python function that is used to check whether the specified path is an existing directory or not. The isdir() function accepts a folder path as an argument and checks if it exists or not. If it exists, then returns True otherwise, False.

What does from OS Path import exists do?

path. exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not.


1 Answers

The dir in download had whitespace at the end while the dir defined in the interactive session did not. The difference was discovered by printing repr(dir).

In [3]: os.path.isdir('/tmp')
Out[3]: True

In [4]: os.path.isdir('/tmp\n')
Out[4]: False
like image 189
unutbu Avatar answered Oct 02 '22 00:10

unutbu