微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 不写入最后一个文件?

如何解决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 举报,一经查实,本站将立刻删除。