Python没有写入最后一个文件?

Python没有写入最后一个文件?,python,function,glob,write,Python,Function,Glob,Write,为了好玩,我做了一个函数,它包含一个文件名、要写的内容和一个位置,这样我就可以让我的代码看起来更干净。但我的问题是它不会写入最后一个文件。我可以删除path6,如果它是最后一个,它将不会写入path5,所以这告诉我我有一个bug,但我无法找出它。谁能给我指一下正确的方向吗?谢谢,这里是我的密码 import os import glob """ Function takes in filename, userInput and folder location th

为了好玩,我做了一个函数,它包含一个文件名、要写的内容和一个位置,这样我就可以让我的代码看起来更干净。但我的问题是它不会写入最后一个文件。我可以删除path6,如果它是最后一个,它将不会写入path5,所以这告诉我我有一个bug,但我无法找出它。谁能给我指一下正确的方向吗?谢谢,这里是我的密码

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, userInput, path)
writeto(fileName, userInput, path2)
writeto(fileName, userInput, path3)
writeto(fileName, userInput, path4)
writeto(fileName, userInput, path5)
writeto(fileName, userInput, path6)

打开并写入文件后,您将更改到目录-因此第一个文件将进入未知目录,而最后一个目录从未写入文件。谢谢,我知道这将是一件简单的事情。我曾想过要更改目录,但我无法控制它。谢谢你根本不需要在这里更改目录。您可以执行打开(location+“/\u Script.bat”,“w”)。尝试在filewrite.close()之前添加filewrite.flush()。@Bohdan为什么?close()执行刷新。