将python var传递给bash

2024-09-27 23:20:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个脚本来拍摄照片并将其写入一个用“data&time”创建/命名的文件夹中

我做这个部分是为了创建目录和拍照

pathtoscript = "/home/pi/python-scripts"
current_time = time.localtime()[0:6]
dirfmt = "%4d-%02d-%02d-%02d-%02d-%02d"
dirpath = os.path.join(pathtoscript , dirfmt)
dirname = dirpath % current_time[0:6] #dirname created with date and time
os.mkdir(dirname) #mkdir
pictureName = dirname + "/image%02d.jpg" #path+name of pictures

camera.capture_sequence([pictureName % i for i in range(9)])

然后我想将dirname传递给bash脚本(picturesToServer),后者将图片上传到服务器。 我该怎么做?你知道吗

cmd = '/home/pi/python-scripts/picturesToServer  >/dev/null 2>&1 &'
call ([cmd], shell=True)

也许我可以留在python脚本中,将图片scp到服务器上?我有一个带有释义集(ssh add mykey)的ssh代理。你知道吗


Tags: path脚本hometimeosscriptspicurrent
1条回答
网友
1楼 · 发布于 2024-09-27 23:20:15

通过将call替换为:

import subprocess    
p = subprocess.Popen(cmd, shell=True, env={"VAR_NAME": dirname})

或者将其作为位置参数传递(在脚本的$1中可用),方法是将cmd替换为:

cmd = '/home/pi/python-scripts/picturesToServer >/dev/null 2>&1 "{0}" &'.format(dirname)

作为旁注,在调用子进程时,请考虑使用shell = True而不是。使用shell = Truea lot of reasons that are documented in the Python docs来说是个坏主意

相关问题 更多 >

    热门问题