Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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 3.x 如何根据文件路径保存文件?_Python 3.x_User Interface_Tkinter - Fatal编程技术网

Python 3.x 如何根据文件路径保存文件?

Python 3.x 如何根据文件路径保存文件?,python-3.x,user-interface,tkinter,Python 3.x,User Interface,Tkinter,我创建了浏览按钮,它显示了我想要什么 这是我的密码 from PyPDF2 import PdfFileReader import tkinter as tk from tkinter import filedialog root = tk.Tk() label_list = [] def get_info(path): with open(path, 'rb') as f: pdf = PdfFileReader(f) info = pdf.ge

我创建了浏览按钮,它显示了我想要什么

这是我的密码

from PyPDF2 import PdfFileReader
import tkinter as tk
from tkinter import filedialog


root = tk.Tk()

label_list = []

def get_info(path):
    with open(path, 'rb') as f:
        pdf = PdfFileReader(f)
        info = pdf.getDocumentInfo()

        label_list[0].config(text=pdf.getNumPages())
        label_list[1].config(text=info.author)
        label_list[2].config(text=info.creator)
        label_list[3].config(text=info.producer)
        label_list[4].config(text=info.subject)
        label_list[5].config(text=info.title)

def browsefunc():
    filename = filedialog.askopenfilename()
    pathlabel.config(text=filename)
    get_info(filename)

browsebutton = tk.Button(root, text="Browse", command=browsefunc)
browsebutton.pack()

pathlabel = tk.Label(root)
pathlabel.pack()

for i in range(6):
    label_list.append(tk.Label(root, text=""))
    label_list[i].pack()


root.mainloop()
如何从文件路径浏览按钮保存或移动到新目录


我真的希望得到您的帮助

听起来您想让浏览窗口从python脚本所在的不同目录位置开始打开(默认行为)

如果是,请将目录名作为
filedialog.askopenfilename()的参数,如下所示:

# Example Directories 
# example_path = os.path.abspath('C:/Users/MyName/Desktop')
# example_path = os.path.abspath('C:/example/cwd/mydir/')
# or 

example_path = os.path.abspath('C:/Windows/Temp')
filename = filedialog.askopenfilename(initialdir=example_path)

将打开该目录中的文件浏览器。

您是否搜索过
python更改目录
?Python中有一个非常好的模块提供了这项服务。是的,但是@riov8没有任何变化。您不能更改调用程序的shell的目录,您可以在您想要的目录中启动一个新shell。