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

2
  • 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) Commented Mar 8, 2013 at 22:03
  • Show your python code which is using stdin. Reading from sys.stdin and printing (to sys.stdout) is correct. Commented Mar 8, 2013 at 22:06

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

This is so intriguing that I am going to go check this out.
Okay, this works for me! Seems as thought I was formatting a few things wrong. Thank you :)
@pydsigner What do you mean exactly? What do you find so intriguing?
Weird, did you mean sys.stdin.read()? I get an attribute error when trying to access sys.read().
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?
|
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(), ...

Comments

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.

2 Comments

wc can handle stream input, as well as function as an independent program. so wc fileName.txt is also doable. @Shayde's program needs to handle both
he had output but no input initially

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.