File explorer - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: File explorer (/thread-37891.html) |
File explorer - DPaul - Aug-04-2022 Hi, In a win11 python 10.x environment, I can start the file explorer from within python like so: import os def show_explorer(path): os.startfile(path) show_explorer(os.path.abspath("c:/windows/explorer.exe"))Almost what I want. I would like it to open on a specific directory outside python. Why? The setup: inside python resides a tree and each extremity is a directory. I want to click on a tree node, automatically navigate to the directory and manipulate it. (paste a pdf, click on a pdf, view a pic...etc) If I do this inside tKinter, I cannot manipulate the directory. Can this be done? thx, Paul RE: File explorer - DeaD_EyE - Aug-04-2022 You can use different kinds of filedialog with tkinter .There are different types of filedialog :
You should also read this: dialog.html#module-tkinter.filedialog Demo Code: from io import IOBase from pathlib import Path from tkinter import Tk from tkinter.filedialog import ( askdirectory, askopenfile, askopenfilename, askopenfilenames, askopenfiles, asksaveasfile, asksaveasfilename, ) from tkinter.messagebox import showinfo dialog_functions = ( askdirectory, askopenfile, askopenfilename, askopenfilenames, askopenfiles, asksaveasfile, asksaveasfilename, ) file_types = ( ("text files", "*.txt"), ("video files", ["*.mp4", "*.mkv", "*.avi"]), ("All files", "*.*"), ) showinfo( "Info", "Visit https://docs.python.org/3/library/dialog.html#module-tkinter.filedialog", ) for func in dialog_functions: # directory has no filetypes title = f"{func.__name__}: {func.__doc__}" if "directory" in func.__name__: ret_val = func( title=title, initialdir=Path.home().joinpath("Downloads"), ) else: # it's a file dialog, so it knows filetypes ret_val = func( title=title, initialdir=Path.home().joinpath("Downloads"), filetypes=file_types, ) showinfo("Title", f"Return value of {func.__name__}\n\n{ret_val}") # closing the FileObject if it's an instance of FileObject if isinstance(ret_val, IOBase): ret_val.close() showinfo("File closed", f"The file '{ret_val.name}' has been closed.") elif isinstance(ret_val, list): for file_obj in ret_val: if isinstance(file_obj, IOBase): file_obj.close() showinfo("Files closed", "The files has been closed") RE: File explorer - DPaul - Aug-04-2022 dear DeaD_EyE, I am familiar with the tKinter filedialogs. All I need is to add some parameter to this line: show_explorer(os.path.abspath("c:/windows/explorer.exe"))So that it goes to a specific dir. I do not need a filedialog to search for it, i know the one I want. But maybe that is impossible. Thx, Paul RE: File explorer - Gribouillis - Aug-04-2022 (Aug-04-2022, 09:10 AM)DPaul Wrote: But maybe that is impossible.You could probably start the explorer with the subprocess module and convenient arguments. Use this page for the syntax of the arguments. Also I don't use Windows but I heard it is getting rid of Internet Explorer. Upgrade to Linux. RE: File explorer - DeaD_EyE - Aug-04-2022 Don't use os.startfile nor os.system for this. from subprocess import Popen def select_object(path): return Popen(["explorer.exe", f"/select,{path}"]) if __name__ == "__main__": select_object(r"C:\Users")If you want more control: https://www.apriorit.com/dev-blog/727-win-guide-to-hooking-windows-apis-with-python (I don't read this, because I'm a Linux user, I don't care much about Windows) RE: File explorer - DPaul - Aug-04-2022 (Aug-04-2022, 09:56 AM)DeaD_EyE Wrote: Don't use os.startfile nor os.system for this.Bingo! It works! thx, Paul RE: File explorer - DPaul - Aug-04-2022 (Aug-04-2022, 09:25 AM)Gribouillis Wrote: Also I don't use Windows but I heard it is getting rid of Internet Explorer. Upgrade to Linux.Really ? When the animals still could speak, some people were already telling me "Unix is the future!" Now it's linux. I'll give you this: I regret DOS. It was wonderful. Paul RE: File explorer - Gribouillis - Aug-04-2022 (Aug-04-2022, 03:22 PM)DPaul Wrote: I regret DOS. It was wonderful.I understand that, DOS was not yet a spyware! RE: File explorer - DeaD_EyE - Aug-04-2022 Windows is actually quite ok, if there is no built-in spyware. The other problem with Windows 11 is the coming loss of control. Soon, Windows will be like the Google Play Store. Application violates against policy => will be automatically deleted. You won't be able to install applications the traditional way in a few years. Only from the MS App Store. I don't expect Microsoft to exist in its current form in the future. We will see. RE: File explorer - DPaul - Aug-05-2022 OK guys, I was only joking. For those who remember, I had a brief encounter with OS/2. Made unix look attractive for a moment. Luckily "windows for workgroups" came along. Everybodu happy. Paul |