如何将文件写入(保存)到目录而不是桌面?

How to write(save) files to a directory instead of desktop?

这是我的代码:

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())

如何获取文件夹名称并创建一个具有该名称的目录,并将文件存储在其中?

使用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()