Python: 程序在资源管理器中打开了错误的文件路径?
Python: Program opens incorrect filepath in explorer?
我试图让我的程序打开用户输入的文件路径。此条目保存在我的 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])
因为您的字符串最后看起来像 'Explorer data[0]',当资源管理器找不到路径数据时[0],它会恢复到用户的默认位置 - 我的文档
我试图让我的程序打开用户输入的文件路径。此条目保存在我的 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])
因为您的字符串最后看起来像 'Explorer data[0]',当资源管理器找不到路径数据时[0],它会恢复到用户的默认位置 - 我的文档