我试图在Python3中的临时目录中创建临时文件,但在windows中运行该程序时,我得到一个[WinError267]目录名无效 我认为这是由于文件命名中的windows“”造成的,但我使用了join功能来命名
with tempfile.TemporaryDirectory() as tdp:
path = os.path.join(tdp, "tempfile.txt")
tfp = open(path, "w+t")
tfp.write("This is a temp file in temp dir")
tfp.seek(0)
print(tfp.read())
由于某些原因,您的代码仅在Windows上通过在打开的文件中使用
with
(您无论如何都应该使用该文件)工作(在修复缩进之后)如果文件名不重要,只需使用
tempfile.TemporaryFile
:相关问题 更多 >
编程相关推荐