os.path.isfile 使用误区
2016-08-25 19:19
411 查看
下列这几条语句,看出什么问题了不?
for file in os.listdir(path):
if os.path.isfile(file) and os.path.splitext(file)[1] == '.txt':
#打开txt文件,并提取数据
冥思苦想,没错啊,为啥 os.path.isfile(file)返回的就是false呢。
>>> os.listdir(path)
['cg.A.1.txt', 'cg.A.128.txt', 'cg.A.16.txt', 'cg.A.2.txt', 'cg.A.256.txt', 'cg.
A.32.txt', 'cg.A.4.txt', 'cg.A.512.txt', 'cg.A.64.txt', 'cg.A.8.txt', 'cg.B.1.tx
t', 'cg.B.128.txt', 'cg.B.16.txt', 'cg.B.2.txt', 'cg.B.256.txt', 'cg.B.32.txt',
'cg.B.4.txt', 'cg.B.512.txt', 'cg.B.64.txt', 'cg.B.8.txt', 'cg.C.1.txt', 'cg.C.1
28.txt', 'cg.C.16.txt', 'cg.C.2.txt', 'cg.C.256.txt', 'cg.C.32.txt', 'cg.C.4.txt
', 'cg.C.512.txt', 'cg.C.64.txt', 'cg.C.8.txt', 'cg.D.128.txt', 'cg.D.16.txt', '
cg.D.256.txt', 'cg.D.32.txt', 'cg.D.512.txt', 'cg.D.64.txt']
>>> files = os.listdir(path)
>>> os.path.isfile(files[1])
False
试验了多次,仍然是False, 我去,什么鬼.....
开始Google,看到一些目录操作,无果....
遂查看python自带帮助,终于找到了答案,泪奔....
Return
an existing regular file. This follows symbolic links, so both
be true for the same path.
注意:path是路径.....
而我传的是一个文件名.
解决方法就是:
>>> os.path.isfile(os.path.join(path,files[1]))
True
搞定!
for file in os.listdir(path):
if os.path.isfile(file) and os.path.splitext(file)[1] == '.txt':
#打开txt文件,并提取数据
冥思苦想,没错啊,为啥 os.path.isfile(file)返回的就是false呢。
>>> os.listdir(path)
['cg.A.1.txt', 'cg.A.128.txt', 'cg.A.16.txt', 'cg.A.2.txt', 'cg.A.256.txt', 'cg.
A.32.txt', 'cg.A.4.txt', 'cg.A.512.txt', 'cg.A.64.txt', 'cg.A.8.txt', 'cg.B.1.tx
t', 'cg.B.128.txt', 'cg.B.16.txt', 'cg.B.2.txt', 'cg.B.256.txt', 'cg.B.32.txt',
'cg.B.4.txt', 'cg.B.512.txt', 'cg.B.64.txt', 'cg.B.8.txt', 'cg.C.1.txt', 'cg.C.1
28.txt', 'cg.C.16.txt', 'cg.C.2.txt', 'cg.C.256.txt', 'cg.C.32.txt', 'cg.C.4.txt
', 'cg.C.512.txt', 'cg.C.64.txt', 'cg.C.8.txt', 'cg.D.128.txt', 'cg.D.16.txt', '
cg.D.256.txt', 'cg.D.32.txt', 'cg.D.512.txt', 'cg.D.64.txt']
>>> files = os.listdir(path)
>>> os.path.isfile(files[1])
False
试验了多次,仍然是False, 我去,什么鬼.....
开始Google,看到一些目录操作,无果....
遂查看python自带帮助,终于找到了答案,泪奔....
os.path.
isfile(path)
Return
Trueif path is
an existing regular file. This follows symbolic links, so both
islink()and
isfile()can
be true for the same path.
注意:path是路径.....
而我传的是一个文件名.
解决方法就是:
>>> os.path.isfile(os.path.join(path,files[1]))
True
搞定!
相关文章推荐
- python中os.path.dirname(__file__)和sys.path[0]使用
- python中的os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- python os.path.isfile()因参数问题判断错误
- python中的os.path.dirname(__file__)的使用
- 记录下os.path.dirname(__file__)使用
- python中的os.path.dirname(__file__)的使用
- python中os.path.dirname(__file__)的使用
- os.path.isfile() 判断问题
- python中由于中文路径引起的os.path.isfile(imgpath) == False问题
- os.path.isfile的错误
- 浅析python3中的os.path.dirname(__file__)的使用
- 记录下os.path.dirname(__file__)使用
- python中的os.path.dirname(__file__)的使用
- 使用GreenDao 报"This check is to prevent accidental file generation into a wrong path."错误
- python中os.path.dirname(__file__)的使用
- python中os.path.dirname(__file__)的使用
- 记录下os.path.dirname(__file__)使用
- 如何使用PathFileExists
- PathFileExists用法--使用#include