【问题标题】:Use a Python Module to Open Explorer on a file使用 Python 模块在文件上打开资源管理器
【发布时间】:2023-03-26 10:57:02
【问题描述】:

我是 python 新手,我在使用位于 Open explorer on a file 的一些非常有用的代码制作模块时遇到了一些困难。

我不知道我做错了什么。

我收到以下错误消息:

第 31 行:C:\Apps\E_drive\Python_win32Clipboard.pdf 第 34 行:r'explorer /select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"' Traceback (大多数 最近通话最后):文件 "P:\Data\VB\Python_MarcsPrgs\Python_ItWorks\Open_Win_Explorer_and_Select_File.py", 第 42 行,在 Open_Win_Explorer_and_Select_Fil(文件路径)文件“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)    

任何帮助将不胜感激。

             Marceepoo

【问题讨论】:

    标签: python subprocess pywin32


    【解决方案1】:

    我复制了您的代码并在我的机器上运行它,发现两个错误。 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)
    

    【讨论】:

    • 我认为您在初始化 Popen_arg 时错过了资源管理器的“/select”参数。
    • 此外,资源管理器命令行中的路径确实应该用引号括起来,以防它包含空格字符。所以Popen_arg的初始化应该是Popen_arg = "explorer /select, \"" + f + "\""
    • @sgerg:/select 选项导致我的机器出错,所以我把它忽略了。至于空白,我想你说得有道理;将编辑。
    • @sgerg:/select 的问题已解决,该选项已与其他编辑一起放回。
    • 亲爱的 GreenMatt 和 srgerg:感谢你们为我解决了这个问题。我很尴尬地说,我一天中的大部分时间都在旋转我的车轮。并且非常沮丧。今晚我会快乐而充满希望地睡觉,而不是沮丧和悲惨。既然我认为您的回答很棒并且他们回答了我的问题,那么我是否应该使用某种方法(除了此评论字段)来表明这一点?
    【解决方案2】:

    我认为问题在于Popen_arg 的初始化。请注意,Popen_arg 的输出值为:

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

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

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

    效果会更好。还要注意这一行:

    Popen_arg = str(Popen_arg)
    

    没有效果,可以安全移除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多