Python 不写入最后一个文件?

问题描述

我制作了一个函数,它接收文件名、要写的内容和有趣的位置,这样我就可以让我的代码看起来更简洁。但我的问题是它不会写入最后一个文件。我可以删除 path6 并且它不会写入 path5 如果它是最后一个,所以这告诉我我有一个错误,但我无法弄清楚。有人能指出我正确的方向吗?谢谢这里我的代码

import os
import glob

"""
Function takes in filename,userInput and folder location
then write to the selected file. Also changes directory to mainDir
"""
def writeto(filewrite,userInput,location):
    filewrite = open("_Script.bat","w",)
    filewrite.write(userInput)
    print("Writing settings to file: " + location + fileName)
    filewrite.close()
    os.chdir(mainDir)
    os.chdir(location)

print("\nCurrent Directory is : " + os.getcwd())

fileName = "__Script.bat"
path = 'C:/Users/Admin/MyDocs/Logs and Sheets 1'
path2 = 'C:/Users/Admin/MyDocs/Logs and Sheets 2'
path3 = 'C:/Users/Admin/MyDocs/Logs and Sheets 3'
path4 = 'C:/Users/Admin/MyDocs/Logs and Sheets 4'
path5 = 'C:/Users/Admin/MyDocs/Logs and Sheets 5'
path6 = 'C:/Users/Admin/MyDocs/Logs and Sheets 6'
mainDir = os.getcwd()
os.chdir(path)

settingsinput = input("\nPlease enter your input: ")


writeto(fileName,path)
writeto(fileName,path2)
writeto(fileName,path3)
writeto(fileName,path4)
writeto(fileName,path5)
writeto(fileName,path6)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)