【发布时间】:2019-02-26 15:32:49
【问题描述】:
在Get a string in Shell/Python using sys.argv这个话题之后,我需要改变我的代码,我需要在main.py中使用一个带有这个函数的子进程:
def download_several_apps(self):
subproc_two = subprocess.Popen(["./readtext.sh", self.inputFileName_download], stdout=subprocess.PIPE)
这是我的文件readtext.sh
#!/bin/bash
filename="$1"
counter=1
while IFS=: true; do
line=''
read -r line
if [ -z "$line" ]; then
break
fi
python3 ./download.py \
-c ./credentials.json \
--blobs \
"$line"
done < "$filename"
还有我的download.py 文件
if (len(sys.argv) == 2):
downloaded_apk_default_location = 'Downloads/'
else:
readtextarg = os.popen("ps " + str(os.getppid()) + " | awk ' { out = \"\"; for(i = 6; i <= NF; i++) out = out$i\" \" } END { print out } ' ").read()
textarg = readtextarg.split(" ")[1 : -1][0]
downloaded_apk_default_location = 'Downloads/'+textarg[1:]
如何在我的 download.py 文件中获取和打印self.inputFileName_download?
我在上一篇文章中使用了@tripleee 回答的 sys.argv,但它不能按我的需要工作。
【问题讨论】:
-
为什么在大量管道和系统调用中使用已弃用的
os.popen?结果将是 awk、shell、python 之间的混合:无法维护。使用纯python -
在主文件中创建一个类,该类保存该命令/会话的所有参数,然后将该类对象共享给其他模块。通过这个类对象可以获取其他模块中的数据
-
要么你有很多死代码,要么你删掉了重要的部分。你从不使用的
IFS是怎么回事?为什么你需要一个子流程?这看起来很像XY problem.
标签: python shell subprocess