0

I need to get all lines in a text file that is passed by the console in python. For example:

I had a file named website.txt that has 3 lines:

https://www.amazon.com/
https://www.amazon.com/
https://www.hbo.com/

This file will be passed by the console when the app is executed.

Example: In a Linux terminal:

cat /home/eric/Documentos/websites.txt | python ./application/app.py

So, each line of this file should be read and for each site, I will be the request.

How can I make it?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    Take a look at [`fileinput`](https://docs.python.org/3/library/fileinput.html) module. – wim Apr 27 '21 at 00:22
  • Thanks a lot @wim I can use sys.stdin in a loop for! –  Apr 27 '21 at 00:29
  • Note that in a *nix shell, a pipe `|` doesn't pass a file, it passes binary data, which in this case is lines of text. – wjandrea Apr 27 '21 at 00:40
  • 1
    Yes @wjandrea this link had my answer! My problem here was that I don't knew how to do this question correctly, so, to find an answer was necessary to explain here with an example. Thanks a lot for you answer! –  Apr 27 '21 at 00:42
  • you should get every line separatelly with normal `input()` or `sys.stdin.readline()` - so you have to run it in loop. And if you want to send to another program connected by `|` then you need only `print()` or `sys.stdout.write()` - eventually you can need `print( ..., flush=True)` – furas Apr 27 '21 at 02:41
  • Instead of `cat file.txt | python app.py` consider `python app.py < file.txt`. It uses one less process, and it's quicker to type. – wim Apr 28 '21 at 18:36

0 Answers0