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?
isfile()
is looking in the current directory. Unless you include the directory name with the file name it has no idea where to find your files.Because you need to join the
dirname
withx
,os.listdir()
just lists the contents directly, the contents do not have full path.Example -
When the full path is not given,
os.path.isfile()
searches in the current directory, hence when you give'.'
toos.listdir()
you get a correct list back.Example -
Lets say some folder -
/a/b/c
- has files -x
andy
in it.when you do -
os.listdir('/a/b/c')
, the list returned looks like -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 andx
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 -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) -
where
os.getcwd()
returns the path to current working directory.