我是一个初学的python用户,并且陷入了两难境地。我的程序应该在特定文件中读写。当写入功能起作用时,读取功能只是擦除文件中的所有文本。
def main():
r = open('C:\\Users\\AM\\Desktop\\cool.txt', 'r')
w = open('C:\\Users\\AM\\Desktop\\cool.txt', 'w')
answer = input('Would you like to read or write? (To leave say "quit")')
if(answer == 'read'):
if(r.mode == 'r'):
contents = r.read()
print(contents)
elif(answer == 'write'):
w.write(input('What would you like to write?'))
w.close()
elif(answer == 'quit'):
quit()
else:
print('Sorry, I do not understand')
main()
感谢您的时间!