poem = '''Program is fun when the work is done if you wanna make your work also fun: use python''' f = file('poem.txt','w') # open for 'w'riting f.write(poem) # write text to file f.close() # close the file f = file('poem.txt') while True: line= f.readline() if len(line) == 0: break print(line) f.close()
运行结果:
经过排查错误发现:file改成open就好了