0

I'm running through some exercises in Python right now, including a simple line counter that can take input from the command line or stdin:

#### line_count1.py ####

import sys

def count_lines(file):
    n = 0
    for line in file:
        n = n + 1
    return n

if len(sys.argv) == 1:
    print("Needs stdin")
    file = sys.stdin
else:
    print("File given at command line")
    file = open(sys.argv[1])

print (count_lines(file))

If I enter a file at the command line, ie python line_count1.py file_with_4_lines.txt, it works great, and I get the output:

File given at command line
4

However, if I enter it so that it DOES need stdin via python line_count1.py, I get the following output:

Needs stdin
_

But never actually does anything with my stdin entry. I can enter file_with_4_lines.txt, but then it just takes it and waits for me to input another stdin line, never breaking out until I have to kill the code in Task Manager.

What would be causing this to happen? From my understanding, as soon as I enter something for stdin that should trigger the rest of the code to go through. But it's not. What am I missing?

2
  • Are you expecting it to read the contents of file_with_4_lines.txt when you type file_with_4_lines.txt in the stdin case? Cause that's not how stdin works... Commented Mar 14, 2017 at 0:35
  • It's supposed to equate the file variable with stdin, which is then used in count_lines(file). Commented Mar 14, 2017 at 0:40

2 Answers 2

1

This is not related to your code, but related to the stdin read behaviour on a terminal. See the following post for more information: https://unix.stackexchange.com/questions/16333/how-to-signal-the-end-of-stdin-input.

EDIT: As @Chase said, the key to terminate stdin on window is Ctrl+Z, and on linux it is Ctrl+D.

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

2 Comments

This doesn't seem to solve it, entering file_with_4_lines.txt ^D doesn't exit stdin, and neither does file_with_4_lines.txt, then ^D on the next line.
TO FUTURE READERS: The problem was that I'm on Windows, not Linux. Windows ends with Ctrl+Z, not Ctrl+D.
1

It sounds like you want to accept a filename from stdin if not given at the command line, when what you're doing right now is attempting to count stdin itself.

If the goal is to process a given file, where the name comes from either stdin or the command line, then the code should be changed to:

if len(sys.argv) == 1:
    # Prompt for and read a single line from stdin to get the desired file name
    filename = input("Needs stdin")  # On Py2, use raw_input, not input
else:
    print("File given at command line")
    # Use argument as filename
    filename = sys.argv[1]

# Open the name provided at stdin or command line
# Use with statement so it's properly closed when you're done
with open(filename) as file:
    print(count_lines(file))

Comments

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.