使用Python模块在文件上打开资源管理器

Mar*_*kin 2 python subprocess pywin32

我是一个python新手,我在使用一些非常有用的代码制作模块时遇到了一些困难: 在一个文件上打开资源管理器.

我无法弄清楚我做错了什么.

我收到以下错误消息:

第31行:C:\ Apps\E_drive\Python_win32Clipboard.pdf第34行:r'explorer/select,"C:\ Apps\E_drive\Python_win32Clipboard.pdf"'Traceback(最近一次调用最后一次):文件"P:\ Data \在Open_Win_Explorer_and_Select_Fil(文件路径)文件中的VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py"第42行文件"P:\ Data\VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py",第35行,在Open_Win_Explorer_and_Select_Fil subprocess.Popen(Popen_arg)文件中" C:\ Python27\lib\subprocess.py",第679行,在init errread,errwrite中)文件"C:\ Python27\lib\subprocess.py",第893行,在_execute_child startupinfo中)WindowsError:[错误2]系统找不到指定的文件

这是我的模块:

"""
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)    
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

             Marceepoo
Run Code Online (Sandbox Code Playgroud)

srg*_*erg 7

我认为问题在于初始化Popen_arg.从输出中注意到值Popen_arg是:

r'explorer /select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"'
Run Code Online (Sandbox Code Playgroud)

这实际上是一个python 原始字符串文字.您希望Popen_arg具有此字符串文字所代表的值,而不是此字符串文字本身.我想如果你改成它

Popen_arg = r'explorer /select, "' + f + '"'
Run Code Online (Sandbox Code Playgroud)

它会更好.还要注意该行:

Popen_arg = str(Popen_arg)
Run Code Online (Sandbox Code Playgroud)

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