簡體   English   中英

在Windows cmd中運行python 3.2腳本時文件I / O出現問題,但在IDLE中卻沒有

[英]problems with file I/O while running python 3.2 scripts in windows cmd, but not in IDLE

我是python noob,在cmd提示符下運行帶有open()函數的程序時遇到問題。 該代碼在具有IDLE的python外殼中按預期運行,但是每次我雙擊對應於腳本的圖標核心打開它(我與python相關聯的.py)時,都會收到類似

what file test.txt (I entered input)
file 2 atest2.txt (I entered intput)
Traceback (most recent call last):
File "C:\\Users\\Matthew\\Desktop\\file_io\\find_differences_in_files.py", line 3, in <module>
f=open(c,"r") # open c
IOError: [Errno 22] Invalid argument: 'test.txt\\r'

在多個類似的程序中也會發生類似的問題,但這是一個示例代碼(僅供參考,此代碼在兩個.txt文件中找到第一個區別),它們在IDLE中起作用,但在cmd中卻不起作用。 有人知道出什么事了嗎?

c=input("what file") # get file 1  
d=input("file 2") # get file 2  
f=open(c,"r") # open c   
g=open(d,"r") # open d  
p=f.readlines() # get every line of f   
q=g.readlines()   
i=0  
while i<len(p) and i<len(q):  
    if p[i]!=q[i]:   
        break # stop counting up  
    i+=1  
x=p[i] # store different line  
y=q[i] # store different line  
j=0  
while j<len(x) or i<len(y):       
    if x[j]!=y[j]:   
        break # stop counting up  
    j+=1  
print("The difference is in line %s column %s" % (i+1,j+1))  
c=input("press enter")

您沒有正確處理行尾字符,IDLE以一種格式為您提供行尾,而cmd.exe發送另一種格式。

我建議在這種情況下從輸入的兩端刪除空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM