try: f = open('data.txt', 'w') ts = f.write() # ← 这里是关键错误! print('写文件正确') except: print('文件操作错误')
在 Python 中,file.write() 方法必须传入一个字符串参数,比如:‘
f.write("Hello World")
但你写的是:
python
ts = f.write() # ❌ 缺少必需的位置参数
TypeError: write() takes exactly one argument (0 given)
write() 方法被调用时没有提供任何要写入的内容,Python 解释器会直接报错 —— 这是一个运行时异常