-2

I have a file "BatchLink.txt" which contains urls in new lines.I want to read these lines by lines and pass the argument linewise to a batch script.

Let's say, my batchlink.txt contains this data :-

http://link1
http://link2
http://link3
http://link4

Now, I want to read linewise and send this to a batch file one line at a time.This question is the continuation of my previous question Here. Right Now, I have this code :-

import requests
from bs4 import BeautifulSoup
import subprocess 

file = open("BatchLinks.txt", "w")
r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})
for episode in subtitles:
  x = episode.find_all('a')
  for a in x:
   #print a['href']

   z = a['href']

  l = 'http://www.crunchyroll.com'+ z
  print l
  file.write(l+'\n')

print '-----------------------------------------------------------------------------'

file.close()

file = open('BatchLinks.txt', "r")
lines = file.readlines()
print lines

if __name__ =="__main__":
    response = lines
    print(lines)
    p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
    time.sleep(1)

    p.stdin.write(response) #Answer the question

    time.sleep(20)

But, right now, the problem is that it reads the lines simultaneosuly and send it to the batch file.Which returns me the output [] . I can't seem to get it work.Any help/guidance would be appreciated.

Community
  • 1
  • 1
Xonshiz
  • 1,307
  • 2
  • 20
  • 48

4 Answers4

2
file = open('BatchLinks.txt', "r")
lines = file.readlines()

Change that to a more up-to-date version:

with open('BatchLinks.txt', "r") as inf:
    for line in inf:
        do something with line
Andrew J
  • 33
  • 6
0
file = open('BatchLinks.txt', "r")
lines = file.readlines()

Change that to a more up-to-date version:

with open('BatchLinks.txt', "r") as inf:
    for line in inf:
        do something with line

This is very basic stuff. Use the manual! https://docs.python.org/2/library/stdtypes.html#file-objects

jcoppens
  • 5,306
  • 6
  • 27
  • 47
0

When you do - lines = file.readlines() - it reads all the lines from the file and returns it as a list , so lines is a list of all the lines in the file.

Then you are doing - p.stdin.write(response) - this sends the complete list over to the other process, you should iterate over the lines and send each line to a new process.

Example -

if __name__ =="__main__":
    for line in lines:
        print(line)
        p = subprocess.Popen("_start.bat", stdin = subprocess.PIPE)
        time.sleep(1)

        p.stdin.write(line.strip())

       time.sleep(20)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

I see two issues: you need to write to the pipe line by line, and need to close the pipe after done.

Replace the following line:

p.stdin.write(response) #Answer the question

with this one:

for line in response:
    p.stdin.write(line)
p.stdin.close()
Hai Vu
  • 37,849
  • 11
  • 66
  • 93