Closure#

Syllabus Week 9: Files#

  • Module os an interface to interact with the operating system

  • Current working directory os.getcwd()

  • Directory list os.listdir()

  • Check if the file exists os.path.isfile(), also os.path.isdir()

  • Relative and absolute paths, os.path.join()

  • Function open() with 'r' and 'w'

  • Statement with and alias as for simplified handling of exceptions

  • Reading methods: read(), readlines()

  • Splitting lines with splitlines()

  • Writing methods: write(), writelines()

  • More on string escape sequences, repr() and strip()

  • Reading csv files as text files

Advanced#

Note about the material in Advanced section. The advanced material contains more some additional topics related to the weeks’s content. You are not required to read this material, and none of the exercises or exam questions will rely on this material.

Advanced 9.1: Other Methods for Reading Files#

Sometimes it might be convenient to read a file line by line, rather than reading the entire file at once. For example, if you have a very large file. The method readline() reads a single line from the file, and moves the file pointer to the next line. To read a whole file line by line, you can use a loop like the one below.

filename = 'week_09_files/mester_jakob.txt'
with open(filename) as file:
    for line in file:
        print(line.strip())

Advanced 9.2: Creating Folders#

To create folders you can use os.mkdir() function, or a more versatile os.makedirs() function. Look up their description on W3Schools: os.mkdir() and os.makedirs(), and try it using the code below.

import os
os.makedirs("my_test_folder/test_folder_1", exist_ok=True)

Advanced 9.3: Append to a File#

Apart from reading and writing to a file, you can also append to a file, if you open it in the append mode.

Advanced 9.4: File Handling in Python#

The module shutil, which is a part of the Python Standard Library may be used for operation on files, and collections of files: copying, moving, deleting, zipping, etc.

For listing content of directories which math a certain pattern, you can use the module glob

In the material for this week, you learned about handling paths using os.path module. There is another module, pathlib that provides a more advanced (object-oriented) way of handling paths and is preferred by experienced Python programmers.

Advanced 9.5: Pickle Files#

There are many other ways to load and save data that we do not have time to cover in the course. One that is often used is using pickle files.

Pickle files are binary files that can be used to save any Python object. They are very useful if you want to save a large object (e.g., a list of lists, dictionaries) and load it again later. You can read more about pickle files in Python documentation for module pickle. Try this example to save a list of lists to a pickle file and read it again. Here we use 'wb' (write binary) and 'rb' (read binary) instead of 'w' and 'r'.

import pickle
my_list = [[1, 2, 3], [4.4, 5.5, 6.6], ['some_string']]
my_dict = {'a': 100, 'b': 200, 'c': 300}
with open('lst.pkl', 'wb') as f:
    pickle.dump((my_list, my_dict), f)
with open('lst.pkl', 'rb') as f:
    loaded_list, loaded_dict = pickle.load(f)
print(loaded_list)
print(loaded_dict)
[[1, 2, 3], [4.4, 5.5, 6.6], ['some_string']]
{'a': 100, 'b': 200, 'c': 300}

Advanced 9.6: Json and Csv Files#

JSON files are text files that consist of key-value pairs, similar to Python dictionaries. They are often used to save some unstructured data, for example settings used for some experiment. The support for JSON files is provided in Python by the module json.

You have seen that CSV files may be treated as text files. However, there is a more convenient way to work with CSV files. The support for CSV files is provided in Python by the module csv.