Read Data From a File (Task)

In this task, you’ll let your wordcount command read text from a regular file in addition to standard input (stdin). It involves using command-line arguments to specify the path to a file to read from. You’ll also ensure that your program can handle the dash (-) character as a special case to signify reading from standard input.

Acceptance Criteria

  • Your program should read text from a file specified as a command-line argument and compute the count of lines, words, and bytes in it.
  • If no command-line argument is provided, then your program should default to reading from standard input.
  • The dash (-) character as an argument should be treated as an indicator to read from standard input.
  • The output should display the counts of lines, words, and bytes, followed by the file name if reading from a regular file.
  • The counts should be aligned and padded with spaces for readability as before.

Examples

Reading from a file specified by the path given as a command-line argument:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.