Python Forum
Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File explorer
#1
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
You can use different kinds of filedialog with tkinter.

There are different types of filedialog:
  • filedialog which returns a str to a file or directory
  • filedialog which returns a list with str
  • filedialog which returns an open FileObject
  • filedialog which returns a list with open FileObjects


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")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
(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.
Reply
#5
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-wi...ith-python
(I don't read this, because I'm a Linux user, I don't care much about Windows)
carecavoador likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Aug-04-2022, 09:56 AM)DeaD_EyE Wrote: Don't use os.startfile nor os.system for this.
Bingo! It works!
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#8
(Aug-04-2022, 03:22 PM)DPaul Wrote: I regret DOS. It was wonderful.
I understand that, DOS was not yet a spyware!
Reply
#9
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
OK guys, I was only joking.
For those who remember, I had a brief encounter with OS/2. Bonkself
Made unix look attractive for a moment.
Luckily "windows for workgroups" came along.
Everybodu happy.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rename files in a folder named using windows explorer hitoxman 3 693 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Odd behavior when starting Explorer.exe malonn 2 2,000 Sep-14-2020, 07:45 PM
Last Post: malonn
  Renaming explorer files in order? stroudie2 2 2,830 Mar-03-2019, 12:41 AM
Last Post: stroudie2
  Looking through files in explorer in order? stroudie2 0 1,667 Mar-02-2019, 11:06 AM
Last Post: stroudie2
  Spyder hides middle array values in variable explorer window davidqvist 0 2,366 Aug-03-2018, 11:04 AM
Last Post: davidqvist
  Spyder IDE - make variable explorer to follow the color scheme of the Editor Antonio 0 5,395 May-05-2018, 10:20 PM
Last Post: Antonio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020