os.path.isfile 没有按预期工作 [英] os.path.isfile isn't working as expected

查看:36
本文介绍了os.path.isfile 没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 os.path.isfile() 检查 Python 中是否存在文件,但尽管文件确实存在,但它返回 false.例如,当我输入 /Users/jordanbaron/Desktop/hero-bg.jpg 这是输出

I am trying to check if a file exists in Python using os.path.isfile(), but it is returning false although the file does exist. For example, when I type /Users/jordanbaron/Desktop/hero-bg.jpg This is the output

Enter the directory to the ISO file (or just drag the file here): /Users/jordanbaron/Desktop/hero-bg.jpg 
/Users/jordanbaron/Desktop/hero-bg.jpg 
<type 'str'>
False

该文件确实存在.

为什么会这样?

filename = raw_input("Enter the directory to the ISO file (or just drag the file here): ")
print(filename)
print(type(filename))
print(os.path.isfile(filename))

推荐答案

您发布的代码有效:

文件存在

/usr/bin/python2.7 /home/surest/github/tests/test.py
Enter the directory to the ISO file (or just drag the file here): /home/surest/Desktop/duties.odt
/home/surest/Desktop/duties.odt
<type 'str'>
True


Process finished with exit code 0

文件名/路径中的错字

 /usr/bin/python2.7 /home/surest/github/tests/test.py
Enter the directory to the ISO file (or just drag the file here): /home/surest/Desktop/meesa-typoed.odt
/home/surest/Desktop/meesa-typoed.odt
<type 'str'>
False

Process finished with exit code 0

这篇关于os.path.isfile 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆