簡體   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