gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:50:58 24 4
gpt4 key购买 nike

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

我不知道我做错了什么。

我收到以下错误消息:

line 31: C:\Apps\E_drive\Python_win32Clipboard.pdf line 34: r'explorer/select, "C:\Apps\E_drive\Python_win32Clipboard.pdf"' Traceback (mostrecent 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_Filsubprocess.Popen(Popen_arg) File "C:\Python27\lib\subprocess.py", line 679, in initerrread, errwrite) File "C:\Python27\lib\subprocess.py", line 893, in _execute_childstartupinfo) 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)

最佳答案

我认为问题在于 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)

没有任何影响,可以安全地删除。

关于python - 使用 Python 模块打开文件资源管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com