# 读取文件 with open('file.txt', 'r') as f: content = f.read() # 写入文件 with open('file.txt', 'w') as f: f.write('Hello, world!') # 检查文件是否存在 import os if os.path.exists('file.txt'): print('File exists')