3

I'm a python newbie, and I'm having some difficulty making a module out of some very useful code located at: Open explorer on a file.

I can't figure out what I am doing wrong.

I get the following error messages:

line 31: C:\Apps\E_drive\Python_win32Clipboard.pdf line 34: r'explorer /select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"' Traceback (most recent call last): File "P:\Data\VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py", line 42, in Open_Win_Explorer_and_Select_Fil(filepath) File "P:\Data\VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py", line 35, in Open_Win_Explorer_and_Select_Fil subprocess.Popen(Popen_arg) File "C:\Python27\lib\subprocess.py", line 679, in init errread, errwrite) File "C:\Python27\lib\subprocess.py", line 893, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified

here's my module:

"""
Open Win Explorer and Select File
# "C:\Apps\E_drive\Python_win32Clipboard.pdf"
"""
import sys
import os, subprocess, pdb

def fn_get_txt_sysarg():
    "Harvest a single (the only) command line argument"
    # pdb.set_trace()
    try:
        arg_from_cmdline = sys.argv[1]
        arg_from_cmdline = str(arg_from_cmdline)
        
    except:
        this_scriptz_FULLName = sys.argv[0]
        
        ErrorMsg = "Message from fn_get_txt_sysarg() in Script (" + this_scriptz_FULLName + '):\n' \
        + "\tThe Script did not receive a command line argument (arg_from_cmdline)"
        
    returnval = arg_from_cmdline

    return returnval


def Open_Win_Explorer_and_Select_Fil(filepathe):
    # harvested from... https://stackoverflow.com/questions/281888/open-explorer-on-a-file
    #import subprocess 
    #subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"') 
    f = str(filepathe)
    print "line 31: " + f
    Popen_arg = "r'explorer /select, " + '"' + f + '"' + "'"
    Popen_arg = str(Popen_arg)
    print "line 34: " + Popen_arg
    subprocess.Popen(Popen_arg)


if __name__ == '__main__': 

    filepath = fn_get_txt_sysarg()
    
    Open_Win_Explorer_and_Select_Fil(filepath)    
tripleee
  • 175,061
  • 34
  • 275
  • 318
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31

2 Answers2

7

I think the problem is with the initialisation of Popen_arg. Notice from the output that the value of Popen_arg is:

r'explorer /select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"'

This is actually a python raw string literal. You want Popen_arg to have the value which this string literal represents, not this string literal itself. I think if you change it to

Popen_arg = r'explorer /select, "' + f + '"'

it will work better. Note also that the line:

Popen_arg = str(Popen_arg)

has no effect and can be safely removed.

srgerg
  • 18,719
  • 4
  • 57
  • 39
4

I copied your code and ran it on my machine and found two errors. The answer srgerg provided addresses one of these errors. The other is that Python triggers an error in fn_get_txt_sysarg() when there is no argument specified on the command line. Below is some sample code, with the errors fixed and some other clean ups:

"""
Open Win Explorer and Select File
"""
import sys
import subprocess

def fn_get_txt_sysarg():
    """Harvest a single (the only expected) command line argument"""
    try:
        return sys.argv[1]    # str() would be redundant here
    except:
        ErrorMsg = 'Message from fn_get_txt_sysarg() in Script (' + sys.argv[0] + '):\n' + '\tThe Script did not receive a command line argument'
        sys.exit(ErrorMsg)

def Open_Win_Explorer_and_Select_Fil(filepath):
    # harvested from: https://stackoverflow.com/questions/281888/open-explorer-on-a-file
    Popen_arg = 'explorer /select,"' + filepath + "'"    # str() is redundant here also
    subprocess.Popen(Popen_arg)

if __name__ == '__main__': 
    filepath = fn_get_txt_sysarg()
    Open_Win_Explorer_and_Select_Fil(filepath)
Community
  • 1
  • 1
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • I think you've missed the "/select," argument to explorer when you initialise `Popen_arg`. – srgerg Nov 22 '11 at 02:40
  • 1
    Also the path in the explorer command line really should be surrounded in quotes, in case it contains white space characters. So the initialisation of Popen_arg should be `Popen_arg = "explorer /select, \"" + f + "\""` – srgerg Nov 22 '11 at 02:46
  • 1
    @sgerg: The /select option caused an error on my machine, so I left it out. As for the white space, I guess you have a point; will edit. – GreenMatt Nov 22 '11 at 03:55
  • 1
    @sgerg: The problem with /select was figured out and the option was put back in with other edits. – GreenMatt Nov 22 '11 at 04:16
  • Dear GreenMatt and srgerg: Thank you both for solving this problem for me. I am embarrassed to say that I had been spinning my wheels for most of the day. and was very frustrated. I'll go to bed happy & hopeful tonight, instead of defeated and miserable. Since I think your answers were great and they answered my question, isn't there some method (other than this comment field) that I should use to indicate that? – Marc B. Hankin Nov 22 '11 at 04:58