Input & Output

Class objectives

Syntax table

A function to create a single directory, returns a directory object.
A function to create a multiple directories, returns a multiple nested directories.
open() returns a file object, and is most commonly used with two arguments: open(filename, mode). The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be ‘r’ when the file will only be read, ‘w’ for only writing (an existing file with the same name will be erased), and ‘a’ opens the file for appending; any data written to the file is automatically added to the end. ‘r+’ opens the file for both reading and writing. The mode argument is optional; ‘r’ will be assumed if it’s omitted.

Create a directory or directories

How to create a directory?

os is a standard library that offers capabilities of creading directories. Following is how to achieve it:

mkdir() method creates a directory, try to create a directory python_basics:

import os
# Create a directory named python_basics


makedirs() creates directories, ‘/’ follows after each directory name:

# Create directories
# Following code create day01 and day02 under directory python_basics

Create a file

open(filename, mode) creates a file object. Look at Key syntax introduction for more details.


  • r : the file will only be read
  • w : only writing an existing file with the same name will be erased
  • a : opens the file for appending
  • r+ : opens the file for both reading and writing
  • b : opens the file in binary mode

How to create a file?

Create a file with open() function.

# Let's create a text file
text_file = open('file.txt', 'w')
How to create a file under a directory?

Read and write file


There are two ways to read/write files:

# Normal approach to write and must close the file with `close()` method.
text_file.write('Happy coding')

# With a `with` keyword
with open('file.txt', 'w') as text_file:
	text_file.write('Python is a great programming language')

What is the difference with or without using the with keyword?

It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:

If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.


# Read them all together:
with open('file.txt', 'r') as file:

# Loop through line by line
file = open('file.txt', 'r')
for each_line in file:

# Read with `readline()` method
with open('file.txt', 'r') as file:

Classroom exercise

Write a function to create a file with the following code:

from turtle import *

def draw_something(sides):
	for each_shape in range(12):
		for each_line in range(sides):

Homework: Create a file hierarchy

Write function(s) to create directories and files to achiece the following file hierarchy in the picture.


import os

# Implement the function
def createfile(filename):

# Implement the funtion
def createdir(dirname):

# Implement the function
def createdirs(dirsname):

# Implement the function
def create_hierarchy():


    {{ $urlPre := "" }} {{ $gistJ := getJSON $urlPre "/users/maninwindow/repos" }} {{ range first 5 $gistJ }} {{ if .public }}
  • {{ .description }}
  • {{ end }} {{ end }}