使用子进程在 Shell/Python 中获取字符串 [英] Get a string in Shell/Python with subprocess

查看:30
本文介绍了使用子进程在 Shell/Python 中获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此主题之后 获取字符串在 Shell/Python 中使用 sys.argv ,我需要更改我的代码,我需要在 main.py 中使用带有此函数的子进程:

After this topic Get a string in Shell/Python using sys.argv , I need to change my code, I need to use a subprocess in a main.py with this function :

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,但它不能按我的需要工作.

How can I get and print self.inputFileName_download in my download.py file ? I used sys.argv as answerd by @tripleee in my previous post but it doesn't work as I need.

推荐答案

好的,我把最后一行改成了:

Ok I changed the last line by :

downloaded_apk_default_location = 'Downloads/'+textarg.split("/")[-1]

获取文本文件名

这篇关于使用子进程在 Shell/Python 中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆