Windows: how to open Explorer window with a specific folder

Asked by Shawn Robertson

All I want to do in this script is just open a windows explorer window to a specific directory so that I can then use the IDE to interact with specific files to right click etc..

before I do that I just want to open "C:\tssmoketest\2014\"

there are several files in here I need to right click on to go to properties etc and i can handle that portion fine.

in my script I have:

import os
import subprocess

tsVersion = input("Enter your version ie.. 2013, 2014 etc:", "2014")

subprocess.Popen('explorer os.path.join('c:', os.sep, 'tssmoketest', tsVersion)')

this doesnt work as my formatting is incorrect.

So i try to get basic with it and just use:

subprocess.Popen('explorer "C:\tssmoketest\2014"')

and this works however it just keeps opening my local user profile documents folder and I dont know why this is.

I then try to use:

os.system('start "C:\tssmoketest\2014"')

and all this does is open a command line terminal with the file path c:\program files (x86)\Sikuli X>

I'm still learning python and reading here and there to learn on the fly so my apologies for the poor coding. Defintely putting notes in my scripts to help retain and learn along the way.

I didn't think just opening a specific windows directory in windows explorer woudl be so hard.

I can do it another way which is:

type('r', KeyModifier.WIN)
wait("Typethenameo.png")
paste(os.path.join('c:', os.sep, 'tssmoketest', tsVersion, 'data01'))
type(Key.ENTER)

but its longer and its ugly and figured I could use somethign similar to someapp.open()

Any help is much appreciated.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Shawn Robertson
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Depending on what you want to do with your files:
There are tons of modules and functions in Python (as in every scripting language), to work with files directly,without fiddling around with rightClick()'s in an explorer window. With Sikuli, this approach is always to be preferred.

dir = os.path.join('c:', os.sep, 'tssmoketest', tsVersion)
for name in os.listdir(dir): # all files in dir
     print name # the bare name of the file
     print os.path.join(dir, name) # the filename with path

most things you can do in the context menu for a file in explorer you can do with Python directly in your script.

But if you really need the explorer window, you need to use the Windows start command, so the os.popen comes back directly to continue your script. the rest is string handling, to setup the correct command for os.popen().

the start command, to open an explorer window:
dir = os.path.join('c:', os.sep, 'tssmoketest', tsVersion)
os.popen('start explorer "%s" ' % dir)

this works even if the path name contains blanks.

if you are sure, your path does not contain blanks (only ASCII characters, numbers, underscores and hyphens):
os.popen("start explorer " + dir)

Revision history for this message
Shawn Robertson (shawn-robertson) said :
#2

thanks so much Raiman. i used this one:

dir = os.path.join('c:', os.sep, 'tssmoketest', tsVersion)
os.popen('start explorer "%s" ' % dir)

which works perfectly.

I would love to know what the %s stands for?

I take that the % sign is to handle spaces and the DIR part is for the defined DIR i set earlier but the %s part is throwing me off.

trying to read some examples on pythons documentation and stack overflow but not really finding a clear definition.

Revision history for this message
RaiMan (raimund-hocke) said :
#3
Revision history for this message
Shawn Robertson (shawn-robertson) said :
#4

fantastic! added to my collection of studies. thank you Raiman.