📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
Edit on GitHub
  1. 11 - File I/O

File management

Filenames and paths

Files are organized into directories (also called folders). Every running program has a current directory, which is the default directory for most operations. For example, when you open a file for reading, Python looks for it in the current directory.

The os module provides functions for working with files and directories (os stands for operating system''). os.getcwd() returns the name of the current directory:

>>> import os 
>>> cwd = os.getcwd()
>>> print(cwd) 
 /home/lblot
>>>

cwd stands for "current working directory". The result in this example /home/lblot, which is the home directory of a user named lblot.

A string like cwd that identifies a file is called a path. A relative path starts from the current directory; an absolute path starts from the topmost directory in the file system. The paths we have seen so far are simple filenames, so they are relative to the current directory. To find the absolute path to a file, you can use os.path.abspath:

>>> os.path.abspath('memo.txt')
 '/home/dinsdale/memo.txt' 
>>>

os.path.exists checks whether a file or directory exists:

>>> os.path.exists('memo.txt') 
 True 
>>>

If it exists, os.path.isdir checks whether it's a directory:

>>> os.path.isdir('memo.txt') 
 False 
>>> os.path.isdir('music')
 True 
>>> os.path.isfile('memo.txt') 
 True 
>>> 

Similarly, os.path.isfile checks whether it's a file.

os.listdir returns a list of the files (and other directories) in the given directory:

>>> os.listdir(cwd)
 ['music', 'photos', 'memo.txt'] 
>>> 

To demonstrate these functions, the following example "walks" through a directory, prints the names of all the files, and calls itself recursively on all the directories.

import os

def walk(dir): 
    for name in os.listdir(dir): 
        path = os.path.join(dir, name)
        if os.path.isfile(path):
            print(path)    
        else:
            walk(path)

os.path.join takes a directory and a file name and joins them into a complete path.

Exercise: Modify walk so that instead of printing the names of the files, it returns a list of names.

Answer
import os

def walk(dir): 
    name_list = []
    for name in os.listdir(dir): 
        path = os.path.join(dir, name)
        if os.path.isfile(path):
            name_list.append(path)  
        else:
            name_list += walk(path)
    return name_list

A common error is to use a return statement within the for loop. This would terminate the recursion prematurely.

Exercise: The os module provides a function called walk that is similar to this one but more versatile. Read the documentation and use it to print the names of the files in a given directory and its subdirectories.

PreviousRead/Write to a fileNextDebugging

Last updated 1 year ago