1
cat hello.txt | python main.py foobar

what is the reason of using cat here?

I understand foobar is command line argument so I need to handle it with arguments parser. how to handle it if i do not specify parameter e.g. --parameters ?

what about hello.txt used after cat?

is it the name of the file that i am passing into python main.py call as other argument or am I dumping python main.py execution results into hello.txt? how and what exactly? am I catching what python main.py is printing with cat and writing it into hello.txt?

EDIT:

Am i capturing it correct?

import argparse
import sys


def parse_arguments():
    parser = argparse.ArgumentParser()

    parser.add_argument('a', nargs='*')

    args, unknown = parser.parse_known_args()
    return args

file_content = data = sys.stdin.read()
params = parse_arguments()

print(file_content)
print(params)
naivepredictor
  • 898
  • 4
  • 14
  • this is basic os shell. That says run "cat" which prints out the contents of "hello.txt" and then pipe that to the standard input of "python main.py ..." (same as if you just ran python, and then used the keyboard) – Kenny Ostrom Jul 27 '19 at 16:01
  • Hey, thank you for the comment. I understand the first part but I do not get how to catch the file or its content in within main.py. – naivepredictor Jul 27 '19 at 16:16
  • What file? It's just the standard input. This might help: https://stackoverflow.com/questions/29454365/what-does-sys-stdin-read – Kenny Ostrom Jul 27 '19 at 16:19
  • hello.txt is a file with some content. when you call cat hello.txt then you print it to the screen. how to capture it in main.py? – naivepredictor Jul 27 '19 at 16:21
  • No, it's not a file any more. It replaced sys.stdin (that happened in the operating system, before python started) – Kenny Ostrom Jul 27 '19 at 16:25
  • I have just updated my question. It is a file all the time as it is created as hello.txt. I understand calling it with cat prints file content to stdin – naivepredictor Jul 27 '19 at 16:25
  • Or just use https://stackoverflow.com/questions/5404068/how-to-read-keyboard-input – Kenny Ostrom Jul 27 '19 at 16:25
  • No mate, I do not want to use keyboard input. I am asking how to capture in main.py stdin print results of call cat on a file and you provide me sth else. I am asking for bike and you advice me to use a bus. – naivepredictor Jul 27 '19 at 16:26
  • This is basic os shell. Look up piping to standard input. Your keyboard has been replaced with the contents of "hello.txt" (This is an operating system question, not a python question.) – Kenny Ostrom Jul 27 '19 at 16:27
  • Ok, I have solved the part with cat hello.txt but I am still unsure about foobar part – naivepredictor Jul 27 '19 at 16:41
  • https://docs.python.org/3.3/library/argparse.html or just look at sys.argv I found those and several tutorials googling "python command line arguments" – Kenny Ostrom Jul 27 '19 at 17:53

0 Answers0