Python,尝试从命令提示符运行程序
我正在尝试从 Windows 中的命令提示符运行程序。我遇到一些问题。代码如下:
commandString = "'C:\Program Files\WebShot\webshotcmd.exe' //url '" + columns[3] + "' //out '"+columns[1]+"~"+columns[2]+".jpg'"
os.system(commandString)
time.sleep(10)
因此,使用单引号,我得到“文件名、目录名或卷标语法不正确”。如果我用 \" 替换单引号,那么它会说“'C:\Program' 不是有效的可执行文件。”
我意识到这是一个语法错误,但我不太确定如何解决这个问题。 ...
列 [3] 包含从 Web 浏览器粘贴的完整 url 副本(因此应该对它进行 url 编码)。列 [2] 包含一些文本、双引号和冒号。以防万一提及...
谢谢!
I am trying to run a program from the command prompt in windows. I am having some issues. The code is below:
commandString = "'C:\Program Files\WebShot\webshotcmd.exe' //url '" + columns[3] + "' //out '"+columns[1]+"~"+columns[2]+".jpg'"
os.system(commandString)
time.sleep(10)
So with the single quotes I get "The filename, directory name, or volume label syntax is incorrect." If I replace the single quotes with \" then it says something to the effect of "'C:\Program' is not a valid executable."
I realize it is a syntax error, but I am not quite sure how to fix this....
column[3] contains a full url copy pasted from a web browser (so it should be url encoded). column[1] will only contain numbers and periods. column[2] contains some text, double quotes and colons are replaced. Mentioning just in case...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
subprocess
模块而不是os.system
,这样更加健壮,并且避免了直接调用shell,让你不必担心令人困惑的转义问题。+
将长字符串放在一起。使用字符串格式化(string %s" % (formatting,)
),这样更易读、更高效、更惯用。subprocess.call
,//
的字符串文字。无论如何,您应该使用 os.path 模块来避免解析转义造成的任何混乱,并且通常使脚本更可移植。subprocess
module rather thanos.system
, which is more robust and avoids calling the shell directly, making you not have to worry about confusing escaping issues.+
to put together long strings. Use string formatting (string %s" % (formatting,)
), which is more readable, efficient, and idiomatic.subprocess.call
.//
has both slashes in the string it makes. In any event, rather than either you should use theos.path
module which avoids any confusion from parsing escapes and often makes scripts more portable.使用 subprocess 模块来调用系统命令。另外,尝试删除单引号并使用双引号。
Use the subprocess module for calling system commands. Also ,try removing the single quotes and use double quotes.