Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文件写入(保存)目录而不是桌面?_Python_Directory - Fatal编程技术网

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

Python 如何将文件写入(保存)目录而不是桌面?,python,directory,Python,Directory,这是我的密码: 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.di

这是我的密码:

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创建一个具有该名称的目录,并将文件存储在其中?

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