9.2 Files

  • A text file is a sequence of characters
  • A binary file (for images, videos and more) is a sequence of bytes
  • First character in a text file or byte in a binary file is located at position 0
    • In a file of n characters or bytes, the highest position number is n – 1
  • For each file you open, Python creates a file object that you’ll use to interact with the file

Conceptual view of a file

End of File

  • Every operating system provides a mechanism to denote the end of a file
    • Some use an end-of-file marker
    • Others maintain a count of the total characters or bytes in the file
    • Programming languages hide these operating-system details from you

Standard File Objects

  • When a Python program begins execution, it creates three standard file objects:
    • sys.stdin—the standard input file object
    • sys.stdout—the standard output file object, and
    • sys.stderr—the standard error file object.
  • Though considered file objects, they do not read from or write to files by default
    • The input function implicitly uses sys.stdin to get user input from the keyboard
    • Function print implicitly outputs to sys.stdout, which appears in the command line
    • Python implicitly outputs program errors and tracebacks to sys.stderr, which also appears in the command line
  • Import the sys module if you need to refer to these objects explicitly in your code—this is rare

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.