这是我的代码:
f = open("myfile.txt")
f.write("Writing something")
f.close()
plt.savefig('plot1.png')
plt.savefig('plot2.png')
workbook = xlsxwriter.Workbook('results.xlsx')
workbook.close()
目前,这些文件正在保存到我的桌面。我如何将它们保存到用户指定名称的文件中?所以我会有一个像这样的小部件:
self.directoryname= tkinter.Entry(self.master)
self.directoryname["width"] = 60
self.directoryname.focus_set()
self.directoryname.grid(row=1, column=1)
foldername = (self.directoryname.get())
我如何获取foldername并使用该名称创建目录,并将文件存储在那里?
答案 0 :(得分:0)
使用os.path.join()
功能,如果要创建文件夹os.makedirs()
import os
path = "some/path/to/location"
foldername = (self.directoryname.get())
file_path = os.path.join(path,foldername)
os.makedirs(file_path)
filename = 'results.xlsx'
full_path = os.path.join(file_path,filename)
workbook = xlsxwriter.Workbook(full_path)
workbook.close()