创建文件和将文件写入目录的问题
Issues with creating and writing files to a directory
代码:
import os.path
lines="hiya"
Question_2=input("Do you want a numeric summary report for? Y/N:")#If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
print("Q2-YES")
OutputFolder = input('Name:')
x = os.mkdir(OutputFolder)
save_path = r'C:\Users\Owner\{}'.format(x)
ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
f=open(completeName,"w+")
f.write(lines)
f.close()
回溯:
FileNotFoundError Traceback (most recent call last)
<ipython-input-13-83f8db356fab> in <module>
10 ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
11 completeName = os.path.join(save_path, ReportName)
---> 12 f=open(completeName,"w+")
13 f.write(lines)
14 f.close()
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Owner\None\testr.txt'
我正在尝试将条件 .txt 文件写入我目录中创建的文件夹,由于某种原因,该文件夹仅在我的 jupyter notebook 中创建,而不是在所需目录中创建,因此出现“找不到目录错误” .有谁知道我如何更改它在所需目录中创建文件夹的代码?提前谢谢你
问题出在行
x=os.mkdir(OutputFolder)
os.mkdir
没有明确地 return 一个值,所以 x
变成了 None
。当您将 x
插入 save_path
时,它被转换为字符串 'None'
。这创建了一个不存在的目录路径,因此 Python 无法在其中创建文件。
错误是因为在第 8 行中您将 x 分配给 os.mkdir 而不是 return 文件名,因此请传递您要创建目录的路径。
我认为这就是您正在寻找的答案:
import os.path
lines="hiya"
Question_2=input("Do you want a numeric summary report for? Y/N:")#If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
print("Q2-YES")
OutputFolder=input('Name:')
save_path = r'C:\Users\Owner\{}'.format(OutputFolder)
os.mkdir(save_path)
ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
f=open(completeName,"w+")
f.write(lines)
f.close()
我还做了一些更改来简化这段代码。主要是使用 with statement 这里是简化:
import os.path
lines="hiya"
Question_2 = input("Do you want a numeric summary report for? Y/N:") # If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
OutputFolder = input('Enter Output folder name: ')
save_path = os.path.abspath('C:\Users\owner\{}'.format(OutputFolder))
os.mkdir(save_path)
ReportName = input("Name of report file:") + ".txt" # Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
with open(completeName, "w") as output_file:
output_file.write(lines)
代码:
import os.path
lines="hiya"
Question_2=input("Do you want a numeric summary report for? Y/N:")#If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
print("Q2-YES")
OutputFolder = input('Name:')
x = os.mkdir(OutputFolder)
save_path = r'C:\Users\Owner\{}'.format(x)
ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
f=open(completeName,"w+")
f.write(lines)
f.close()
回溯:
FileNotFoundError Traceback (most recent call last)
<ipython-input-13-83f8db356fab> in <module>
10 ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
11 completeName = os.path.join(save_path, ReportName)
---> 12 f=open(completeName,"w+")
13 f.write(lines)
14 f.close()
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Owner\None\testr.txt'
我正在尝试将条件 .txt 文件写入我目录中创建的文件夹,由于某种原因,该文件夹仅在我的 jupyter notebook 中创建,而不是在所需目录中创建,因此出现“找不到目录错误” .有谁知道我如何更改它在所需目录中创建文件夹的代码?提前谢谢你
问题出在行
x=os.mkdir(OutputFolder)
os.mkdir
没有明确地 return 一个值,所以 x
变成了 None
。当您将 x
插入 save_path
时,它被转换为字符串 'None'
。这创建了一个不存在的目录路径,因此 Python 无法在其中创建文件。
错误是因为在第 8 行中您将 x 分配给 os.mkdir 而不是 return 文件名,因此请传递您要创建目录的路径。
我认为这就是您正在寻找的答案:
import os.path
lines="hiya"
Question_2=input("Do you want a numeric summary report for? Y/N:")#If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
print("Q2-YES")
OutputFolder=input('Name:')
save_path = r'C:\Users\Owner\{}'.format(OutputFolder)
os.mkdir(save_path)
ReportName=input("Numeric Summary Report Name.txt:")#Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
f=open(completeName,"w+")
f.write(lines)
f.close()
我还做了一些更改来简化这段代码。主要是使用 with statement 这里是简化:
import os.path
lines="hiya"
Question_2 = input("Do you want a numeric summary report for? Y/N:") # If you input Y it will generate a single .txt file with number summary
if Question_2 == 'Y' or 'y':
OutputFolder = input('Enter Output folder name: ')
save_path = os.path.abspath('C:\Users\owner\{}'.format(OutputFolder))
os.mkdir(save_path)
ReportName = input("Name of report file:") + ".txt" # Name Your Report ".txt"
completeName = os.path.join(save_path, ReportName)
with open(completeName, "w") as output_file:
output_file.write(lines)