I want to check whether a file exists using Python, C:/test/UpdatePackage/filelist
is an existing file.
When run in sublime text 3, the result is wrong:
>>> f = "C:/test/UpdatePackage/filelist"
>>> import os
>>> os.path.isfile(f)
False
>>>
But run in Python 3.4 GUI
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> f = "C:/test/UpdatePackage/filelist"
>>> import os
>>> os.path.isfile(f)
True
>>>
in addition to timgeb's answer:
I haven't tried it, but os.path.isfile(os.path.normpath(f))
should give you the expected behaviour. use normpath()
, then you can use either \\
or /
as separator and your code is (more) independent from your platform or interpreter (eg if you get the path from user input or other sources).
Use f = "C:\\\\test\\\\UpdatePackage\\\\filelist"
In your script. I believe IDLE automagically replaces the forward slashes for you with os.sep , that's why it is working inside IDLE.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.