简体   繁体   English

将参数从一个python脚本传递给另一个

[英]Passing argument from one python script to another

I've got 3 files in total : clean.txt, origin.py and pump.py 我总共有3个文件:clean.txt,origin.py和pump.py

Clean.txt has some lines in it (actually website links. For eg: Clean.txt中有几行(实际上是网站链接。例如:

www.link1.com
www.link2.com
www.link3.com
www.link4.com

origin.py is the script that's reading lines one by one and I want this script to send the link (one at a time) to pump.py origin.py是一个正在逐行读取脚本的脚本,我希望该脚本将链接(一次一个)发送到pump.py。

pump.py is a script which asks me for one input, which is a link. pump.py是一个脚本,要求我输入一个输入,这是一个链接。

Now, I want to make it so that I read lines from clean.txt (origin.py is doing this task) and send them to the pump.py one at a time. 现在,我想这样做,以便我从clean.txt中读取行(origin.py正在执行此任务),然后一次将它们发送到pump.py。 Like a loop. 就像一个循环。 Code for Origin.py is : Origin.py的代码是:

fo = open("Clean.txt", "r")
nremoval = str(fo.readlines()).replace('\\n','')
      for lines in fo :
          if __name__ =="__main__":
             response = nremoval
             p = subprocess.Popen("pump.py", stdin = subprocess.PIPE)
             time.sleep(2) 
             p.stdin.write(bytes(response, 'ascii'))
             print("injected string :", response)

origin.py origin.py

import subprocess
import time

# open file 
with open("Clean.txt", "r") as fo:

     # read line by line
     for line in fo:

         # remove ENTER and SPACES from single line 
         line = line.strip() 

         # call "python pump.py" 
         p = subprocess.Popen(["python","pump.py"], stdin=subprocess.PIPE)

         # or "python3 pump.py"
         #p = subprocess.Popen(["python3","pump.py"], stdin=subprocess.PIPE)

         #time.sleep(2) 

         # send line to standard input
         #p.stdin.write(bytes(line, 'ascii')) # python 3
         p.stdin.write(line) # python 2

         print("injected string:", line)

pump.py pump.py

#line = input() # python 3
line = raw_input() # python 2

print("received:", line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM