在当前目录中写入新文件夹并将文件写入i

2024-09-27 04:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的脚本,它将向新目录写入一个输入文件。我想更改它,这样我就不必显式地告诉它它在哪个目录中才能编写一个新目录。当前的方法工作正常,但需要用户输入路径。我看到了下面的New folder that is created inside the current directory,并按以下方式修改了我的脚本

import os

if not os.path.isdir('/home/bkwx97/python/test3/MINP'):
    os.mkdir('/home/bkwx97/python/test3/MINP')


with open('/home/bkwx97/python/test3/MINP/MINP','w') as f:

得到了错误

"File "test3backupdir2.py", line 8, in with open('final_directory/MINP.txt','w') as f: FileNotFoundError: [Errno 2] No such file or directory: 'final_directory/MINP.txt'"

我错过了一些简单的事情吗?或者我可以不更改它,这样它将在当前目录中写入一个新文件夹,然后在那里写入输入?在

^{pr2}$

Tags: 文件方法目录txt脚本homeosas

热门问题