Open explorer on a file

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.


Asked by: Haris181 | Posted: 05-10-2021






Answer 1

From Geoff Chappell's The Windows Explorer Command Line

import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')

Answered by: Kelvin258 | Posted: 06-11-2021



Answer 2

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.

Answered by: Arnold318 | Posted: 06-11-2021



Answer 3

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])

Answered by: Thomas884 | Posted: 06-11-2021



Answer 4

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()

Answered by: Byron695 | Posted: 06-11-2021



Answer 5

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)

Answered by: Alberta169 | Posted: 06-11-2021



Answer 6

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

Answered by: Dominik499 | Posted: 06-11-2021



Answer 7

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.

Answered by: Maddie929 | Posted: 06-11-2021



Answer 8

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)

Answered by: Emma323 | Posted: 06-11-2021



Similar questions

python - Can't Open file in explorer

i have checked here, and tried the code independently and it worked...but inserting the code in my QTreeView app...it opens up the explorer user libraries with 'My Documents' in focus i use subprocess.Popen(r'explorer /select,"file_path"')...


python - open file in finder or explorer on linux or unix

I don't have any Mac OSx or Linux machine, but I want to implement the functionality like opening an explorer from a selected file path. for example import subprocess subprocess.Popen('explorer "E://temp//"') the above code opens windows explorer for a specified path, how to do it for Mac or Linux ?


How to get python to read a file that has been opened through File Explorer?

I don't exactly know how to phrase this question, but I have tried my best. Let's say I have an application and I have set it to be the default to run when the .example file extention has been double clicked within File Explorer. Now, how do I get this application not to just launch, but instead to react as if it has been asked to open the file. Now I know there is the: file = open ("C:\\Ex...


python - why I cannot run bzr explorer

I have python as seen in which python and also ➜ ~ brew install qt Warning: qt-4.8.6 already installed ➜ ~ brew install pyqt Warning: pyqt-4.10.4 already installed but then I try to run on my OSX: ➜ ~ bzr explorer and gets this error: invalid syntax (<string>, line 1) Unable to load u...


python - Open a folder in explorer if not opened yet

I know how to open a folder in explorer with python via : subprocess.Popen(r'explorer /select,"C:\path\of\folder"') But I don't know how to prevent my program from opening the folder if it is already "opened" in explorer. Is there a way to do this in Python (or via a VBA script maybe) ?


python - Google API Explorer on local host - 404 Not Found

I am running a webapp locally using dev_appserver.py My app is running properly on localhost:8080 When I try to access the Api endpoints explorer @ localhost:8080/_ah/api/explorer , I get 404 Error Console log: INFO 2017-11-07 05:59:24,864 module.py:821] default: "GET /_ah/api/explorer HTTP/1.1" 404 154 app.yaml application: hari-mq-...


python - Django Unit Tests doesn't run in Visual Studio Test Explorer

I'm trying to implement my Django (1.11.7, Python 3.6) unit testing in Visual Studio. When I run them in my terminal (python manage.py test) they run fine, while when executing from the VS Test Explorer it fails with an error saying: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. I've read that newer Django versions require an explicit django.setup() to be called, so I...


python - Can't Open file in explorer

i have checked here, and tried the code independently and it worked...but inserting the code in my QTreeView app...it opens up the explorer user libraries with 'My Documents' in focus i use subprocess.Popen(r'explorer /select,"file_path"')...


python - I cant find file explorer on Spyder

I can't seem to find file explorer on python to make a file my directory only variable explorer


python - Select Tab in Helium Explorer Page

Im trying to have Selenium select the Activity tab on this page but cant figure out how I would select that tab. Here is the url: https://explorer.helium.com/hotspots/112YabQcCywBonqZ8hgg213DPCvRxYxN3knSccAWfkakPjyBcaJL import bs4 from selenium import webdriver ...


Is there a way for python to access a text file that can’t be access in file explorer?

I am making a program that uses text files on a network drive and I don’t want anyone to access the text file from file explorer. I have already hidden the file but I have realised that most people on the network have hidden folders shown. How would I do this?


Drag and drop onto Python script in Windows Explorer

I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target. Is there some kind of configuration that needs to be done somewhere for this work?


python - open file in finder or explorer on linux or unix

I don't have any Mac OSx or Linux machine, but I want to implement the functionality like opening an explorer from a selected file path. for example import subprocess subprocess.Popen('explorer "E://temp//"') the above code opens windows explorer for a specified path, how to do it for Mac or Linux ?


Python (2.6) ftplib didn't connect to site, but FTP worked from IE Explorer

I'm not certain if I'm misunderstanding something about FTP, or if there is something funny about the site I was trying to FTP into. When I tried: import ftplib HOST = 'www.site.org' USER = <user name> PASSWD = <password> FTPConn = ftplib.FTP(HOST,USER,PASSWD) the result was: error:[Errno 10060] A connection attempt failed because the connected party did...


explorer - Open a file browser in a specific folder with Python

I'm a french guy, so excuse me if my English is not very good. Here is my problem : I'm using python to make a script that works with Blender, and I need to import an .obj file. I already have the importer, but I enter the entire filepath. It's look like that : bpy.ops.import_scene.obj(filepath='C:/Users/MarcPro/Desktop/testauto/03-Reconstruction/Data/Tile/Tile.obj', axis_forward='Y', axis_up='Z') ...


eclipse - When I use Mylyn with PyDev I see the context in the task but python files are not shown in any explorer

I use mylyn 3.9.0 with PyDev 2.7.5 with the PyDev Mylyn integration 0.4.0. Mylyn seems to build up contexts correctly (I can see the context tree in the task/context tab). But the python files are not shown "PyDev Package Explorer" nor in the "Project Explorer". What could prevent the python files to appear? Uninstalling the PyDev Mylyn integration did not help.


python - Pydev Package Explorer Issues

I have just installed Pydev on my existing copy of Eclipse for Java, running on OSX 10.7. When I make a new pydev package, I click away from the default directory for my java files and into one for python files. Once it is created, the project shows in the pydev package explorer window. However, once I close the package, it disappears from the pydev explorer window, but shows in the java explorer window (under java pers...


Open windows explorer at a specific network location in Python

I have tried so many variants of a theme to get this explorer window open at the P:\ drive, from what my little knowledge tells me, the fact the path to the folder is anywhere but the C:\ drive means it fails (it works with C:) so perhaps the path is wrong? the code below shows some of the tries i have made but still no luck, "P:" is mapped the same on all machines. def Open_Win_Explorer_and_Select_Dir(): ...


In python is there a way to use the windows explorer to create a file path which can then be returned as a string?

I am making a program to manage a database and i need a nice way of choosing the save locations of files within the program. I was wondering if it is possible to open windows explore from my program, select a folder to save a file in, enter the file name and return the file path as a string to the main program.


How to get python to read a file that has been opened through File Explorer?

I don't exactly know how to phrase this question, but I have tried my best. Let's say I have an application and I have set it to be the default to run when the .example file extention has been double clicked within File Explorer. Now, how do I get this application not to just launch, but instead to react as if it has been asked to open the file. Now I know there is the: file = open ("C:\\Ex...


How can set Windows Explorer startup path with python

I want to set Windows Explorer startup path with python. like this. %windir%\explorer.exe C:\Users.This is a global setting. it’s need pywin32 or cmd or regedit? please help me。 thanks






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top