Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...