Skip to main content

Files and Exceptions

  • Chapter
  • First Online:
Book cover The Python Workbook

Part of the book series: Texts in Computer Science ((TCS))

  • 12k Accesses

Abstract

When files are used effectively programs can read the data that they need from disk instead of requiring the user to enter it each time the program runs. Similarly, files allow the results generated by a program to be stored in a more permanent manner than printing the output on the screen. Both file input and file output are commonly used by programs that operate on larger amounts of data. This chapter explores the following topics related to files:

  • Opening a file for reading, writing or appending,

  • Reading data from a file,

  • Writing data to a file, and

  • Closing a file.

In addition, this chapter also explores the use of command line arguments and exceptions, both in programs that make use of files and in those that do not. The chapter concludes with 24 exercises that allow the reader to put all of these concepts into practice.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 49.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD 54.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    The character or sequence of characters used to denote the end of a line in a text file varies from operating system to operating system. Fortunately, Python automatically handles these differences and allows text files created on any widely used operating system to be loaded by Python programs running on any other widely used operating system.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Ben Stephenson .

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Springer Nature Switzerland AG

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Stephenson, B. (2019). Files and Exceptions. In: The Python Workbook. Texts in Computer Science. Springer, Cham. https://doi.org/10.1007/978-3-030-18873-3_7

Download citation

  • DOI: https://doi.org/10.1007/978-3-030-18873-3_7

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-030-18872-6

  • Online ISBN: 978-3-030-18873-3

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics