0

i'm trying to automate the research about a list of domain i have (this list is a .txt file, about 350/400 lines). I need to give the same command (that uses a py script) for each line i have in the txt file. Something like that:

import os

with open('/home/dogher/Desktop/copia.txt') as f:
    for line in f:
        process(line)
        os.system("/home/dogher/Desktop/theHarvester-master/theHarvester.py -d "(line)" -l 300 -b google -f "(line)".html") 

I know there is wrong syntax with "os.system" but i don't know how to insert the text in the line, into the command.. Thanks so much and sorry for bad english..

eyllanesc
  • 190,383
  • 15
  • 87
  • 142

4 Answers4

1
import os
with open('data.txt') as f:
for line in f:
    os.system('python other.py ' + line)

If the contents of other.py are as follows:

import sys
print sys.argv[1]

then the output of the first code snippet would be the contents of your data.txt.
I hope this was what you wanted, instead of simply printing by print, you can process your line too.

0

Your approach subjects each line of your file to evaluation by the shell, which will break when (not if) it comes across a line with any of the characters with special meaning to the shell: spaces, quotes, parentheses, ampersands, semicolons, etc. Even if today's input file doesn't contain any such character, your next project will. So learn to do this correctly today:

for line in openfile:
    subprocess.call("/home/dogher/Desktop/theHarvester-master/theHarvester.py", 
         "-d", line, "-l", "300", "-b", "google", "-f", line+".html")

Since the command line arguments do not need to be parsed, subprocess will execute your command without involving a shell.

alexis
  • 43,587
  • 14
  • 86
  • 141
0

Due to the Linux tag i suggest you a way to do what you want using bash

process_file.sh:

#!/bin/bash

#your input file
input=my_file.txt

#your python script
py_script=script.py

# use each line of the file `input` as argument of your script     
while read line
do
  python $py_script $line
done < "$input"

you can access the passed lines in python as follow:

script.py:

import sys
print sys.argv[1]
Gabrio
  • 238
  • 1
  • 3
  • 15
0

Hope below solution will be helpful for you :

with open('name.txt') as fp:
    for line in fp:
        subprocess.check_output('python name.py {}'.format(line), shell=True)

Sample File I have used :

name.py

import sys
name = sys.argv[1] 
print name

name.txt:

harry
kat
patrick
shubhadeep
  • 13
  • 5