0

I'm working on a python 3 project that does the exact same thing as bash's 'wc' (Word count) command. The format for input looks like this:

$ python3 myWC.py < fileName.txt

I'd like to use the file given to count over the bytes, words, and lines in the file and print them. I've tried using stdin but even simple print() statements seem to be printing stdin's object instead of the actual string given. Could someone give me some guidance on basic stdin usability in Python 3? Thank you!

As pointed out below, I was using '>' instead of '<'. But now when I run this code:

import sys

def main():
        word = sys.stdin.readline()
        print(word)

main()

I simply get a blank line as output in Terminal

Shayde
  • 13
  • 3
  • 4
    Um... The > operator is the output forwarder. So I guess if you want to input fileName.txt you should use < and then I guess you can use stdin? (I have no experience with this in python, but I am sure that > is for output and < is for input) – MrHug Mar 08 '13 at 22:03
  • Show your python code which is using stdin. Reading from sys.stdin and printing (to sys.stdout) is correct. – dsh Mar 08 '13 at 22:06

3 Answers3

4

Expanding on my comment above, I would say the following code would do the trick (NOTE: this is not Python 3, but Python 2.7. Some quick Googling told me they are similar in this however).

In echo.py:

import sys
print sys.stdin.read()

Then in your terminal call it like this:

python echo.py < test.txt

This should echo the contents of test.txt on your terminal.

MrHug
  • 1,315
  • 10
  • 27
  • This is so intriguing that I am going to go check this out. – pydsigner Mar 08 '13 at 22:09
  • Okay, this works for me! Seems as thought I was formatting a few things wrong. Thank you :) – Shayde Mar 08 '13 at 22:10
  • @pydsigner What do you mean exactly? What do you find so intriguing? – MrHug Mar 08 '13 at 22:10
  • Weird, did you mean `sys.stdin.read()`? I get an attribute error when trying to access `sys.read()`. – pydsigner Mar 08 '13 at 22:12
  • Oops I did mean that... Time to get some sleep I guess, thanks for catching it :) – MrHug Mar 08 '13 at 22:13
  • @pydsigner Okay, one more thing. I modeled my code as you said, however when I run the program, a single empty line is printed, nothing else. – Shayde Mar 08 '13 at 22:22
  • 1
    Are you using read or readline? Because if the first line is blank and you use readline() then that should result in a blank line. If it is not blank, than strange things occur... Maybe you can post your input file aswell? – MrHug Mar 08 '13 at 22:29
0

how to handle the input inside python is covered in How do you read from stdin in Python?

first line of your input is likely blank and you are only reading one line, try:

import sys
for line in sys.stdio:
    print line

Also I should mention for *nix style utilities the module fileinput worth looking at.

Community
  • 1
  • 1
cmd
  • 5,754
  • 16
  • 30
0

A different approach from the redirection: use fileinput:

# myWC.py
def main():
    for line in fileinput.input():
        # Do something with the line

main()

How to invoke:

python myWC.py filename.txt # One file
python myWC.py file1.txt file2.txt file3.txt # multiple files

Discussion

  • fileinput takes care of all the details of opening file, read them line by line for you
  • You can specify more than one input files from the command line
  • See doc for more information, especially on fileinput.filename(), fileinput.lineno(), ...
Hai Vu
  • 37,849
  • 11
  • 66
  • 93