繁体   English   中英

在sublime text3和IDLE中运行我的Python代码,但结果不一样

[英]Run my Python code in sublime text3 and in IDLE,But the results are not the same

我想使用Python检查文件是否存在, C:/test/UpdatePackage/filelist是否已存在。

当以崇高的文字3运行时,结果是错误的:

>>> f = "C:/test/UpdatePackage/filelist"
>>> import os
>>> os.path.isfile(f)
False
>>> 

但是可以在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
>>>

除了timgeb的答案:

我没有尝试过,但是os.path.isfile(os.path.normpath(f))应该会给您预期的行为。 使用normpath() ,则可以使用\\/作为分隔符,并且代码(更多)独立于平台或解释器(例如,如果从用户输入或其他来源获得路径)。

在脚本中使用f = "C:\\\\test\\\\UpdatePackage\\\\filelist" 我相信IDLE会用os.sep自动替换正斜杠,这就是为什么它在IDLE内部起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM