【发布时间】:2016-03-20 15:28:36
【问题描述】:
我正在编写一个将参数传递给 shell 脚本的 python 程序。
这是我的python代码:
import subprocess
Process=subprocess.Popen('./copyImage.sh %s' %s str(myPic.jpg))
还有我的“copyImage.sh”:
#!/bin/sh
cp /home/pi/project/$1 /home/pi/project/newImage.jpg
我可以毫无问题地在终端上运行脚本。但是在执行python代码时,终端返回了"NameError: name 'myPic' is not defined"。
如果我将语法更改为
Process=subprocess.Popen('./copyImage.sh %s' %s "myPic.jpg")
然后终端返回"OSError: [Errno 2] No such file or directory"。
我已经关注了这个:Python: executing shell script with arguments(variable), but argument is not read in shell script,但它没有帮助。
【问题讨论】:
-
您可以在python本身中复制文件。
shutil.copy2('/home/pi/project/myPic.jpg', '/home/pi/project/newImage.jpg')。无需复杂的子流程调用。