该错误通常出现在尝试在文本文件上执行shell操作时。要解决此问题,请确保在执行shell命令之前将文本文件关闭,例如:
with open('file.txt', 'w') as f: f.write('hello')
f.close()
os.system('ls')
或者,您可以使用subprocess模块来执行shell命令,例如:
import subprocess
with open('file.txt', 'w') as f: f.write('hello')
subprocess.run(['ls'], shell=True)
这应该避免了在执行shell命令时发生的任何问题。
上一篇:AttributeError:'xml.etree.ElementTree.Element'objecthasnoattribute'_children'--notadupe
下一篇:AttributeError:Can'tgetattribute'_unpickle_block'(Mydumpandloadversionsofpandasaresame)