In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders:
import subprocess
subprocess.Popen('explorer "C:\path\of\folder"')
but I have no solution for files.
In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders:
import subprocess
subprocess.Popen('explorer "C:\path\of\folder"')
but I have no solution for files.
From Geoff Chappell's The Windows Explorer Command Line
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
works, but how do I get this window on top of other windows, if the user goes to some other window after having executed this statement somewhere in his program? –
Gershon 1
even though the explorer window opens correctly and selects the file. Does anyone know why this is, maybe worth a question in its self? (I get a return code of 1
if I use the string command with os.system
, subprocess.call
, or subprocess.Popen and ask for the return code.) –
Hairbrush comma (,)
between /select
and the path
then the My Documents
is opened. i.e. explorer /select,"C:\path\of\folder\file"
is correct not this explorer /select "C:\path\of\folder\file"
–
Gilkey subprocess.Popen(fr'explorer /select,"{my_var}"')
–
Hepatica A nicer and safer solution (only in Windows unfortunately) is os.startfile().
When it's given a folder instead of a file, it will open Explorer.
Im aware that i do not completely answer the question since its not selecting a file, but using subprocess
is always kind of a bad idea (for security reasons) and this solution may help other people.
startfile
only exists on Windows (#29823528). –
Paederast os.startfile()
does not accept arguments ☹ Although in case of the Explorer it seems easy to have the process detached via subprocess.run
or Popen
–
Geithner ./some/folder
. you may use os.path.abspath("./some/folder")
to convert the path first. –
Uncanny cmd=r'explorer /select,"C:\path\of\folder\file"'; subprocess.Popen(cmd)
if the string cmd is compromised by an evil user it can for example create a new file (example on window): 'cmd /c type NUL > 1.txt'
. With more imagination there is a lot of evil things to do, and that's why i think playing with subprocess is not a good idea. –
Varicotomy As explorer
could be overridden it would be a little safer to point to the executable directly. (just had to be schooled on this too)
And while you're at it: use Python 3s current subprocess API: run()
import os
import subprocess
FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')
def explore(path):
# explorer would choke on forward slashes
path = os.path.normpath(path)
if os.path.isdir(path):
subprocess.run([FILEBROWSER_PATH, path])
elif os.path.isfile(path):
subprocess.run([FILEBROWSER_PATH, '/select,', path])
r'C:\path\file.exe'
, C:\\path\\file.exe
and C:/path/file.exe
they all work. You just need to make sure your path is properly formatted and os.path.isfile
works on it. –
Geithner path = os.path.normpath(path)
is for :) –
Geithner For some reason, on windows 7 it always opens the users Path, for me following worked out:
import subprocess
subprocess.call("explorer C:\\temp\\yourpath", shell=True)
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
works, but how do I get this window on top of other windows, if the user goes to some other window after having executed this statement somewhere in his program? –
Gershon shell=True
tho! security.openstack.org/guidelines/… –
Geithner Alternatively, you could use the fileopenbox module of EasyGUI to open the file explorer for the user to click through and then select a file (returning the full filepath).
import easygui
file = easygui.fileopenbox()
For anyone wondering how to use a variable in place of a direct file path. The code below will open explorer and highlight the file specified.
import subprocess
subprocess.Popen(f'explorer /select,{variableHere}')
The code below will just open the specified folder in explorer without highlighting any specific file.
import subprocess
subprocess.Popen(f'explorer "{variableHere}"')
Ive only tested on windows
subprocess.Popen(fr'explorer "{variableHere}"')
–
Miley import os
path = "C:\path\of\folder"
os.startfile(path)
using this cmd you can go to the path in the file explorer
Code To Open Folder In Explorer:
import os
import ctypes
SW_SHOWDEFAULT = 10
path_to_open = os.getenv('windir')
ctypes.windll.shell32.ShellExecuteW(0, "open", path_to_open, 0, 0, SW_SHOWDEFAULT)
import subprocess
subprocess.Popen(r'explorer /open,"C:\path\of\folder\file"')
I find that the explorer /open command will list the files in the directory. When I used the /select command (as shown above), explorer opened the parent directory and had my directory highlighted.
import os
os.system('notepad filename')
Example 1. If I have a file no.txt in same directory
os.system('notepad no.txt')
Example 2. If I want to open file in some other directory
os.system('notepad "C:\\Users\\DELL\\Downloads\\a.txt"')
Note: I am running this on windows thats why I am using notepad, you can replace according to your os.
This is not entirely an answer to the question, but it helped me so I thought it might help others too.
If you use are using wxPython/wxWidgets, you can try the wx.LaunchDefaultApplication
and wx.LaunchDefaultBrowser
methods. I'm not sure how they behave on Windows, but on my Linux setup they both open my default file manager if I provide a local path that points to a directory as the document
or url
parameter, respectively.
© 2022 - 2024 — McMap. All rights reserved.