2011-11-22 202 views
2

我是一名python新手,我在使用位于Open explorer on a file的一些非常有用的代码时难以制作模块。使用Python模块在文件上打开资源管理器

我搞不​​清楚我做错了什么。

我收到以下错误信息:

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

这里是我的模块:

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

任何帮助将非常感激。

   Marceepoo 

回答

6

我觉得问题在于Popen_arg的初始化。从输出的Popen_arg值是注意:

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

这实际上是一个Python raw string literal。你希望Popen_arg具有这个字符串字面值表示的值,而不是这个字符串字面本身。我想如果你改成

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

它会工作得更好。另请注意,该行:

Popen_arg = str(Popen_arg) 

没有效果,可以安全地删除。

3

我复制了你的代码,并在我的机器上运行它,发现有两个错误。 answer srgerg provided解决了这些错误之一。另一个是当命令行中没有指定参数时,Python在fn_get_txt_sysarg()中触发错误。下面是一些示例代码,修复了错误和一些其他清理:

""" 
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) 
+0

我认为你在初始化'Popen_arg'时错过了资源管理器的“/ select”参数。 – srgerg

+0

另外,资源管理器命令行中的路径应该用引号括起来,以防包含空格字符。所以Popen_arg的初始化应该是'Popen_arg =“explorer/select,\”“+ f +”\“”' – srgerg

+0

@sgerg:/ select选项在我的机器上导致了一个错误,所以我将它遗漏了。至于白色空间,我想你有一点;将编辑。 – GreenMatt

相关问题