简体   繁体   English

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

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

I want to check whether a file exists using Python, C:/test/UpdatePackage/filelist is an existing file. 我想使用Python检查文件是否存在, C:/test/UpdatePackage/filelist是否已存在。

When run in sublime text 3, the result is wrong: 当以崇高的文字3运行时,结果是错误的:

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

But run in Python 3.4 GUI 但是可以在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: 除了timgeb的答案:

I haven't tried it, but os.path.isfile(os.path.normpath(f)) should give you the expected behaviour. 我没有尝试过,但是os.path.isfile(os.path.normpath(f))应该会给您预期的行为。 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). 使用normpath() ,则可以使用\\/作为分隔符,并且代码(更多)独立于平台或解释器(例如,如果从用户输入或其他来源获得路径)。

Use f = "C:\\\\test\\\\UpdatePackage\\\\filelist" In your script. 在脚本中使用f = "C:\\\\test\\\\UpdatePackage\\\\filelist" I believe IDLE automagically replaces the forward slashes for you with os.sep , that's why it is working inside IDLE. 我相信IDLE会用os.sep自动替换正斜杠,这就是为什么它在IDLE内部起作用。

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

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