2011-03-25 75 views
0

我是一個python noob,並且在cmd提示符下使用open()函數運行程序時出現問題。該代碼按預期運行在IDLE python的外殼,但每次我通過雙擊該圖標coresponding到腳本(我的.py與蟒蛇相關),我得到這樣在Windows cmd中運行python 3.2腳本時出現文件I/O問題,但在IDLE中未出現

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'

類似問題的錯誤打開發生在多個類似的程序中,但這裏是一個代碼示例(FYI,這段代碼找到兩個.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") 
+0

另一方面,只允許變量名由一個單個字符組成的BASIC時代已經過去。通過使用更具吸引力的名字讓自己開心。 – 2011-03-25 07:12:45

回答

1

您不能正確處理換行符,並且IDLE以一種格式提供換行符,而cmd.exe會以另一種格式提供換行符。

我建議在這種情況下從輸入的兩端剝去空白。

+0

謝謝。我剛剛使用c = c.strip()剝離空白,現在它在cmd中工作。 – 2011-03-25 06:21:50

相關問題