-1

so in my linux terminal I type python3 main.py input.txt > output.txt

input.txt contains:

This is line 1
This is line 2
This is another line

How do I go through this line by line and print out each line so my output.txt looks like the output below? Essentially, I'm just asking how do I read this like by line? I can't use fopen since I am making input.txt like my stdin

This is line 1
---
This is line 2
---
This is another line
---

My question is different from the rest because here I am not opening a file using open(). I am pipping the stdin input as input.txt. As you can see in the command input.txt > output.txt. So I cannot use open.

Jake
  • 33
  • 4
  • Possible duplicate of [How to read a large file line by line](https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line) – wjandrea Jul 02 '19 at 03:01
  • 1
    I see your edit, but you're confused. `input.txt` is an argument. `output.txt` is connected to stdout. If you wanted to connect `input.txt` to stdin, you would write `< input.txt`. – wjandrea Jul 02 '19 at 03:07
  • @wjandrea No it isn't. You aren't being given the file as an argument. I cant use `sys.argv` because my command is `python3 main.py input.txt > output.txt`. Notice the `>` in the command – Jake Jul 02 '19 at 03:08
  • @wjandrea well when I use `sys.argv` it doesn't have `input.txt` in it. It just has `main.py` – Jake Jul 02 '19 at 03:08
  • 1
    With the command line you gave, `sys.argv` will be `['main.py', 'input.txt']`. – wjandrea Jul 02 '19 at 03:11

1 Answers1

0

With this command line, python3 main.py input.txt > output.txt, this will do what you want:

import sys

for line in open(sys.argv[1]):
    print(line.rstrip('\n'))
    print('---')

However, you have your shell syntax confused. You're expecting input.txt to be connected to stdin, but that would be written python3 main.py < input.txt > output.txt (note the less-than sign <). Then your main.py would look like this:

import fileinput

for line in fileinput.input():
    print(line.rstrip('\n'))
    print('---')

Here I'm using fileinput to iterate over each line of stdin.

By the way, fileinput can get a list of filenames from sys.argv, so this would also work for the command line python3 main.py input.txt > output.txt.

wjandrea
  • 16,334
  • 5
  • 30
  • 53