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就好了
本文展示了一个简单的Python脚本,用于创建并读取包含一首关于编程乐趣的诗歌的文本文件。通过使用Python内置的文件操作功能,该脚本演示了如何打开文件进行写入,然后再次打开文件进行读取和打印。
287

被折叠的 条评论
为什么被折叠?



