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

3 投票
2 回答
5120 浏览
提问于 2025-04-17 06:48

我是一名Python新手,最近在把一些很有用的代码做成模块时遇到了一些困难,这些代码在这里可以找到:打开文件所在的资源管理器

我不知道自己哪里出错了。

我收到了以下错误信息:

第31行:C:\Apps\E_drive\Python_win32Clipboard.pdf 第34行:r'explorer /select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"' 回溯(最近的调用最后): 文件 "P:\Data\VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py",第42行,在 Open_Win_Explorer_and_Select_Fil(filepath) 文件 "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)    

2 个回答

4

我复制了你的代码并在我的电脑上运行,发现了两个错误。srgerg提供的答案解决了其中一个错误。另一个错误是,当命令行没有指定参数时,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)
7

我觉得问题出在 Popen_arg 的初始化上。你可以从输出中看到 Popen_arg 的值是:

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

这实际上是一个 Python 的 原始字符串字面量。你希望 Popen_arg 拥有这个字符串字面量所代表的值,而不是这个字符串字面量本身。我认为如果你把它改成

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

这样会更好。此外,注意这一行:

Popen_arg = str(Popen_arg)

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

撰写回答