这有点奇怪,但我正在运行一个名为scrapebox的程序,scrapebox有一个automator插件,可以创建一个文件来自动运行一些内容.为了从cmd运行automator,我将cd进入程序目录,然后键入:
Scrapebox.exe "automator:1.sbaf"
Run Code Online (Sandbox Code Playgroud)
它首先会启动Scrapebox程序,一旦打开,就会立即运行自动化文件.
这是一个更大拼图中的一小部分.我试图在更大的Python脚本中调用它.
import os
import subprocess
..........
..........
..........
print "Opening Scrapebox now, please wait."
os.chdir('C:\Users\Admin\DomainDB\Programs\ScrapeBox')
print
print "Current working dir : %s" % os.getcwd()
print
subprocess.call(["Scrapebox.exe"])
#"automator:1.sbaf"
print "Scrapebox finished. Moving on."
Run Code Online (Sandbox Code Playgroud)
当我像上面那样运行它时,它可以工作并打开scrapebox.但是,我真正需要做的是这样的事情:
subprocess.call(["Scrapebox.exe "automator:1.sbaf""])
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它会抛出语法错误.那么我如何输入可能作为原始字符串,就好像它被输入cmd一样?
如果要在字符串中嵌入双引号,可以使用多种方法之一.另外要传递所有参数的单个字符串,不要作为列表传递[]:
subprocess.call("Scrapebox.exe \"automator:1.sbaf\"")
subprocess.call('Scrapebox.exe "automator:1.sbaf"')
Run Code Online (Sandbox Code Playgroud)
Python可以在字符串周围使用单引号或双引号.您还可以对一个字符串进行三重引号(在开头和结尾三个单引号或双引号),这也允许换行符,但这里不需要它.
如果传递参数列表,则每个参数都应该是列表的元素:
subprocess.call(['Scrapebox.exe','automator:1.sbaf'])
Run Code Online (Sandbox Code Playgroud)