Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.isfile() doesn't work. Why?

Tags:

python

listdir

I'm trying to do this:

import os
[x for x in os.listdir('.') if os.path.isfile(x)]
[x for x in os.listdir('dirname') if os.path.isfile(x)]
[x for x in os.listdir(os.path.abspath('dirname')) if os.path.isfile(os.path.abspath(x))] 

The first line works:

[x for x in os.listdir('.') if os.path.isfile(x)]

But the next two:

[x for x in os.listdir('dirname') if os.path.isfile(x)]

and

[x for x in os.listdir(os.path.abspath('dirname')) if os.path.isfile(os.path.abspath(x))] 

just output []

Why?

like image 338
Rolex Avatar asked Aug 22 '15 14:08

Rolex


People also ask

How does OS path Isfile work?

The os. path. isfile() function takes the path of a file as a parameter and checks whether the given path contains a valid file. This function returns 'true' when the given path is a regular file and returns 'false' if the given path is not a regular file.

How does Isfile work in Python?

isfile() method in Python is used to check whether the specified path is an existing regular file or not. Parameter: path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.

How do I check if a file exists in Python?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How do you check if a file is a symlink in Python?

islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not. Note: If symbolic links are not supported by the Python runtime then os. path. islink() method always returns False.


1 Answers

Because you need to join the dirname with x , os.listdir() just lists the contents directly, the contents do not have full path.

Example -

[x for x in os.listdir('dirname') if os.path.isfile(os.path.join('dirname',x))]

When the full path is not given, os.path.isfile() searches in the current directory, hence when you give '.' to os.listdir() you get a correct list back.


Example -

Lets say some folder - /a/b/c - has files - x and y in it.

when you do - os.listdir('/a/b/c') , the list returned looks like -

['x','y']

Even if you give absolute path inside os.listdir() , the files returned in the list would have relative path to the dir. You would manually need to join dir and x to get the correct results.


In your third example, it does not work because os.path.abspath() also works with current directory, so if you do something like -

os.path.abspath('somefile')

The result produced would be - /path/to/current/directory/somefile - it does not validate if that is a real file/dir or not.

It is clearly stated in the documentation (Emphasis mine) -

os.path.abspath(path)

Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).

where os.getcwd() returns the path to current working directory.

like image 111
Anand S Kumar Avatar answered Sep 28 '22 02:09

Anand S Kumar