I am a python noob, and am having problems running programs with open() functions in cmd prompts. The code runs as expected in the python shell with IDLE, but everytime I open it by doubleclicking the icon coresponding to the script( I have .py associated with python), I get errors like
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'
Similar problems occur in multiple similar programs, but here is a sample of code( FYI, this code finds the first difference in two .txt files) that worked in IDLE but not in cmd. Anybody have any clue what is going wrong?
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")
You're not handling end-of-line characters correctly, and IDLE gives you the end-of-line in one format, while the cmd.exe sends another.
I suggest stripping whitespace from both ends of the input in this case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.