11 Dealing with files

 

This chapter covers

  • Reading and writing files
  • Processing tabulated data files
  • Preserving data as files
  • Managing files on your computer
  • Accessing file metadata

Files are integral to any computer system or application. We use files to store data. We share data with our teammates by using files. When we obtain a file from others, we need to open the file, read its content, process the data, and write some data to another file or append data to the same file. These operations are concerned with the contents of the files. Our application can use hundreds of different Python objects, and some objects require excessive calculations or other processing steps, so it’s ideal that we can save these objects as files. When we need to use these objects again, we can load them from files, which can save lots of processing time.

Files are everywhere in any computer system, and our job can include many kinds of file manipulations, such as moving files to a destination, extracting files of a specific kind, and finding out the files that we’ve modified in the last week. Adequate knowledge of performing these operations in a programmatic way allows us to perform jobs that we can’t do easily in a manual way and track any changes that we’ve made to the files. In this chapter, we’ll cover important topics concerning files—not only reading and writing from a content perspective, but also common file operations such as moving and copying files.

11.1 How do I read and write files using context management?

11.1.1 Opening and closing files: Context manager

11.1.2 Reading data from a file in different ways

11.1.3 Writing data to a file in different ways

11.1.4 Discussion

11.1.5 Challenge

11.2 How do I deal with tabulated data files?

11.2.1 Reading a CSV file using csv reader

11.2.2 Reading a CSV file that has a header

11.2.3 Writing data to a CSV file

11.2.4 Discussion

11.2.5 Challenge

11.3 How do I preserve data as files using pickling?

11.3.1 Pickling objects for data preservation