从 Python 调用 lp 导致脚本退出
Call to lp from Python causes script to exit
以下 Python 脚本的任务是拍摄照片,然后将其打印出来。但是,每次脚本成功调用lp后,脚本退出(干净利落,无异常,无解释)
import time
import picamera
import subprocess
def main():
with picamera.PiCamera() as cam:
cam.start_preview(alpha=220)
#cam.resolution = (2592, 1944)
cam.capture('test.jpg')
subprocess.check_call("lp -d HP-270 test.jpg")
while True:
main()
time.sleep(5.000)
尝试将对 lp 的调用及其参数分离到一个字符串列表中。这就是您将命令和参数传递给 shell 的方式,默认情况下 subprocess.check_call
没有 shell 解释您提供给它的原始字符串。
subprocess.check_call(["lp", "-d", "HP-270", "test.jpg"])
Subprocess library: Frequently Used Arguments
args is required for all calls and should be a string, or a sequence
of program arguments. Providing a sequence of arguments is generally >
preferred, as it allows the module to take care of any required
escaping > and quoting of arguments (e.g. to permit spaces in file
names). If passing a single string, either shell must be True (see
below) or else the string must simply name the program to be executed
without specifying any arguments.
以下 Python 脚本的任务是拍摄照片,然后将其打印出来。但是,每次脚本成功调用lp后,脚本退出(干净利落,无异常,无解释)
import time
import picamera
import subprocess
def main():
with picamera.PiCamera() as cam:
cam.start_preview(alpha=220)
#cam.resolution = (2592, 1944)
cam.capture('test.jpg')
subprocess.check_call("lp -d HP-270 test.jpg")
while True:
main()
time.sleep(5.000)
尝试将对 lp 的调用及其参数分离到一个字符串列表中。这就是您将命令和参数传递给 shell 的方式,默认情况下 subprocess.check_call
没有 shell 解释您提供给它的原始字符串。
subprocess.check_call(["lp", "-d", "HP-270", "test.jpg"])
Subprocess library: Frequently Used Arguments
args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally > preferred, as it allows the module to take care of any required escaping > and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.