gpt4 book ai didi

Python:程序在资源管理器中打开不正确的文件路径?

转载 作者:行者123 更新时间:2023-12-01 04:00:50 25 4
gpt4 key购买 nike

我试图让我的程序打开用户输入的文件路径。该条目保存在我的 data [0] 列表中,然后我使用 os.system 函数调用该列表。资源管理器不会打开保存的文件路径,而是打开并将我带到“我的文档”。 1.为什么会发生这种情况? 2.如何更改此设置以使资源管理器打开已保存的用户数据?

import Tkinter as tk
import pickle
import os


data = []

class MyPath(tk.Frame):


def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.pack()
MyPath.make_widgets(self)

def make_widgets(self):
self.parent.title("MyPath")

#self.textbox = Text(root)
#self.textbox.pack()

# create a prompt, an input box, an output label,
# and a button to do the computation
self.prompt = tk.Label(self, text="Enter your Filepath:", anchor="w")
self.textbox = tk.Text(self)
self.entry = tk.Entry(self)
self.open = tk.Button(self, text="Open", command = self.open)
self.view = tk.Button(self, text="View", command = self.view)
self.submit = tk.Button(self, text="Save", command = self.save)
self.output = tk.Label(self, text="")
self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)

# lay the widgets out on the screen.
self.prompt.pack(side="top", fill="x")
self.entry.pack(side="top", fill="x", padx=20)
self.output.pack(side="top", fill="x", expand=True)
self.textbox.pack(side="top", fill="x")
self.view.pack(side="bottom")
self.submit.pack(side="bottom")
self.clear_button.pack(side="bottom")
self.open.pack(side="top")


def clear_text(self):
self.entry.delete(0, 'end')
self.textbox.delete(1.0, 'end')


def save(self):
# get the value from the input widget

try:
a = self.entry.get()
result = data.append(self.entry.get())

except ValueError:
result = "Please enter filepaths only"

#saves list to pickle file
#pickle.dump(data, open("save.p", "wb"))

# set the output widget to have our result
self.output.configure(text=result)
print (data)

def view(self):
pickle.load(open("save.p", "rb"))
self.textbox.insert("1.0", str(pickle.load(open("save.p", "rb"))))
print pickle.load(open("save.p", "rb"))

def open(self):
#src = "/Users/matt/Documents/Python/MyApp.py"
dst = data
#path = pickle.load(open("save.p", "rb"))
os.system('Explorer "data[0]"')

if __name__ == "__main__":
root = tk.Tk()
MyPath(root).pack(fill="both", expand=True)
root.mainloop()

非常感谢任何帮助!

最佳答案

os.system('Explorer "%s"' % data[0])

因为您的字符串最后看起来像“资源管理器数据[0]”,当资源管理器找不到路径数据[0]时,它会恢复到“用户 - 我的文档”的默认位置

关于Python:程序在资源管理器中打开不正确的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com