2 回答

TA貢獻(xiàn)1827條經(jīng)驗 獲得超9個贊
該錯誤是因為在第 8 行中您將 x 分配給 os.mkdir ,它不會返回文件名,因此請傳遞您想要在其中創(chuàng)建目錄的路徑。
我認(rèn)為這將是您正在尋找的答案:
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 語句是簡化的:
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)

TA貢獻(xiàn)1719條經(jīng)驗 獲得超6個贊
問題出在線路上
x=os.mkdir(OutputFolder)
os.mkdir
沒有顯式返回值,因此x
變?yōu)?code>None. 當(dāng)您x
插入時save_path
,它被轉(zhuǎn)換為字符串'None'
。這創(chuàng)建了一個不存在的目錄路徑,因此 Python 無法在其中創(chuàng)建文件。
添加回答
舉報