Chapter 9: Reading and writing files

Related methods and modules

face Josiah Wang

File objects have several other methods like .tell() (where is the current position of the file pointer?) and .seek() (go to certain positions in the file). We will not discuss them as you will probably not use these much. You can refer to the official documentation to learn more.

There are also some modules in the Python Standard Library that might be useful for working with directories and files:

  • The tempfile module is useful for creating temporary files (without a specific filename).
  • The shutil module provides high level utilities to operate on files. For example, see shutil.copy() and shutil.move().
  • The os module contains useful low-level functions for manipulating files and directories. For example, see os.listdir(), os.makedirs(), os.getcwd(), os.rename(), os.remove().
  • The os.path module contains useful functions for dealing with pathnames. For example, see os.path.exists(), os.path.join(), os.path.basename(), os.path.isdir().
  • The pathlib module provides an object-oriented implementation of some of the functions in os and os.path.

I will not discuss these in detail either. I am confident enough that you will be able to explore and use these on your own, especially when you need them!