我在写一个将参数传递给shell脚本的python程序。将参数传递给来自Python的shell脚本
这里是我的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')'。无需使子流程调用复杂化。 – tdelaney