简体   繁体   English

subprocess.call 路径问题,如何解决?

[英]subprocess.call path problem , how to fix?

my subprocess.call problem is that my shortcut target is with extra INI file which is LIV2.INI and my exe file should run whit it.我的 subprocess.call 问题是我的快捷方式目标是带有额外的 INI 文件,即 LIV2.INI,我的 exe 文件应该运行它。 and my target link in shortcut looks like this我在快捷方式中的目标链接看起来像这样

"C:\Program Files (x86)\AMO\EXE\PROGRAM LIVE 2.exe" LIV2.INI

i tried this我试过这个

subprocess.call('"C:\Users\admin\Desktop\PROGRAM LIVE 2.exe" LIV2.INI')

and i tried this我试过这个

subprocess.call('C:\Users\admin\Desktop\PROGRAM LIVE 2.exe LIV2.INI')

and i still get error that the ini file missing?我仍然收到ini文件丢失的错误? How can i fix this:)我怎样才能解决这个问题:)

THank you in advance先感谢您

ERROR: INI FILE Missing or Wrong Name错误:INI 文件丢失或名称错误

Please also edit your question to actually include the error since you will get a syntax error, not a error that the ini file is missing.还请编辑您的问题以实际包含错误,因为您将收到语法错误,而不是 ini 文件丢失的错误。

You have two issues here, first you have a syntax error since "\Us" is not a valid string in python.这里有两个问题,首先是语法错误,因为“\Us”不是 python 中的有效字符串。 \u marks the start of a Unicode escape sequence and the character S is not a valid unicode escape key. \u 标志着 Unicode 转义序列的开始,字符 S 不是有效的 unicode 转义键。 You can fix this by using double \\ to escape the \ character and tell python you want your string to include a \ and not use it as the start of a escape sequence.您可以通过使用双\\转义\字符并告诉 python 您希望您的字符串包含\并且不将其用作转义序列的开头来解决此问题。

Secondly, subprocess.call excpects a list, not a string.其次, subprocess.call期望一个列表,而不是一个字符串。 (unless you set shell=True but don't do that since it means you have to manually escape things which you have already discovered is hard). (除非您设置shell=True但不要这样做,因为这意味着您必须手动转义已经发现很难的事情)。 First element of the list is the executable to run and the rest are command line arguments.列表的第一个元素是要运行的可执行文件,rest 是命令行 arguments。 For example if you wanted to run python and print hello world you would type:例如,如果您想运行 python 并打印 hello world,您可以输入:

subprocess.call(['python', '-c', 'print ("hello world")'])

Notice the missing quotes around the python string?注意到 python 字符串周围缺少引号了吗? You don't need those since the command line arguments are passed in raw and no shell will attempt to split them if you don't include quotes.您不需要这些,因为命令行 arguments 是原始传递的,如果您不包含引号,则不会尝试拆分它们。

Putting it all together creates something like this:把它们放在一起会产生这样的东西:

subprocess.call(['C:\\Users\\admin\\Desktop\\PROGRAM LIVE 2.exe', 'LIV2.INI'])

Double backslashes and each command line argument as it's own list element.双反斜杠和每个命令行参数作为它自己的列表元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM