4

I was docked points in a coding challenge that specified that I needed to read from STDIN. This was my input method:

def __init__(self, input):
    self._dictionary = {}
    with open(input, 'r') as f:
        reader = csv.reader(f, delimiter='\t')
        for row in reader:
          if self._dictionary.__contains__(row[0]):
            self._dictionary[row[0]].append(row[1])
          else:
            self._dictionary.update({row[0]: row[1].split()})

and at the end of the script

if __name__ == "__main__":
    script = Script(sys.argv[1])
    for line in script.output_method():
      print line

Was I wrong to use sys.argv in a challenge that asked to read from stdin? What's the difference? What should I have done to satisfy the requirements?

BarFooBar
  • 1,086
  • 2
  • 13
  • 32
  • 2
    `STDIN` - wherefrom you will take the input (test cases) for your program. `sys.argv` - the arguments provided to program when running it. – Vivek Rai Oct 03 '14 at 04:02
  • Yeah, I get that. But what about the above script does not satisfy "script must read from STDIN"? – BarFooBar Oct 03 '14 at 04:03
  • 1
    check this out: http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python There are multiple answers in there that come under stdin – user3885927 Oct 03 '14 at 04:09

1 Answers1

8

They are not the same:

>>> import sys
>>> sys.argv == sys.stdin
False

sys.argv

  • The list of command line arguments passed to a Python script.

sys.stdin sys.stdout sys.stderr

  • File objects corresponding to the interpreter’s standard input, output and error streams. stdin is used for all interpreter input except for scripts but including calls to input() and raw_input().

As @Vivek Rai mentioned in the comments, you can use sys.stdin.readlines() to read from standard in. Also, fileinput, is available to you, which seems to do exactly what you want.

import fileinput
for line in fileinput.input():
    process(line)

This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.

monkut
  • 42,176
  • 24
  • 124
  • 155
  • What should I have done to satisfy the requirements? – BarFooBar Oct 03 '14 at 04:09
  • @BarFooBar: You should have read from `stdin` instead of from a user-specified file. – jwodder Oct 03 '14 at 04:10
  • 1
    @BarFooBar Simply do, `sys.stdin.readlines()` to read all lines from the input. – Vivek Rai Oct 03 '14 at 04:13
  • @BarFooBar instead of passing a string as the argument, pass a file object. If sys.argv includes a filename, open and pass that; otherwise pass sys.stdin. I strongly believe in not hard-coding `stdin` except at the highest level in `main` – o11c Oct 03 '14 at 04:17