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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 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
Corresponding author
Rights and permissions
Copyright information
© 2019 Springer Nature Switzerland AG
About this chapter
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)