0% found this document useful (0 votes)
4 views9 pages

Lecture 27

The document discusses using recursion to find the size of a directory by recursively calculating the size of all files and subdirectories. It provides an algorithm that uses recursion to calculate the size and also describes some Python os module functions that are useful for the problem. It then shows Python code to implement the recursive algorithm and provides sample output.

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Lecture 27

The document discusses using recursion to find the size of a directory by recursively calculating the size of all files and subdirectories. It provides an algorithm that uses recursion to calculate the size and also describes some Python os module functions that are useful for the problem. It then shows Python code to implement the recursive algorithm and provides sample output.

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Science 1001

Lecture 27

Lecture Outline

• Recursion

– CS1001 Lecture 27 –
Finding the size of a directory (folder)

• Now, we will look at a problem that is difficult to


solve without using recursion.

• The problem is to find the size of a directory.

• The size of a directory is the sum of the sizes of


all the files in the directory (including those inside
subdirectories inside the directory).

– CS1001 Lecture 27 – 1
A sample directory structure

courses

comp1001 comp1002

lab01 lab02 assign01 assign01 assign02

code example.txt data.dat my_file.txt output.txt


my_code.py
my_code1.py

my_code2.py

• A directory d may contain subdirectories.

• The size of the subdirectories has to be computed


as well.

– CS1001 Lecture 27 – 2
Finding the size of a directory (folder)

Algorithm getSize to get the size of a directory

Input: a file name or a directory name f

1. set size to zero


2. if f is a directory
a. get list of items in f
b. for each item x in f
i. size += getSize(x)
c. return size
3. else if f is a file
return size of f

• Here, the base case is in step 3.

• getSize calls itself with simpler input (subdirectory


or file).

– CS1001 Lecture 27 – 3
The os module

The os module provides several functions that will be


useful for this problem:

• os.path.isfile(f)
returns True if f is a file.

• os.path.isdir(f)
returns True if f is a directory.

• os.path.join(path,f)
returns the full name of f (including path).

• os.path.getsize(f)
returns the size of the file f.

• os.listdir(f)
returns a list of the subdirectories and files under
the directory f.

– CS1001 Lecture 27 – 4
Finding the size of a directory (folder)

import os

def getSize(f):
size = 0
if os.path.isfile(f): # base case - f is a file
return os.path.getsize(f) # add file size
elif os.path.isdir(f): # f is a directory
lst = os.listdir(f) # get contents of f
for i in lst:
new_f = os.path.join(f,i)
size += getSize(new_f) # recursive calls
return size
else: # base case - f does not exist
print(f, " does not exist.")
return size

# Main program
file = input("Enter a file name or directory name: ")
print("The size of ", file, " is ", getSize(file), " bytes")

– CS1001 Lecture 27 – 5
Finding the size of a directory (folder)
Directory courses :
[’comp1002’, ’comp1001’]
Calling getSize on courses/comp1002
Directory courses/comp1002 :
[’assign02’, ’assign01’]
Calling getSize on courses/comp1002/assign02
Directory courses/comp1002/assign02 :
[’output.txt’]
Calling getSize on courses/comp1002/assign02/output.txt
File courses/comp1002/assign02/output.txt size= 46
Calling getSize on courses/comp1002/assign01
Directory courses/comp1002/assign01 :
[’my_file.txt’]
Calling getSize on courses/comp1002/assign01/my_file.txt
File courses/comp1002/assign01/my_file.txt size= 34
Calling getSize on courses/comp1001
Directory courses/comp1001 :
[’lab02’, ’lab01’, ’assign01’]
Calling getSize on courses/comp1001/lab02
Directory courses/comp1001/lab02 :
[’my_code.py’]
Calling getSize on courses/comp1001/lab02/my_code.py
File courses/comp1001/lab02/my_code.py size= 30
Calling getSize on courses/comp1001/lab01
Directory courses/comp1001/lab01 :
[’example.txt’, ’code’]
Calling getSize on courses/comp1001/lab01/example.txt
File courses/comp1001/lab01/example.txt size= 20
Calling getSize on courses/comp1001/lab01/code
Directory courses/comp1001/lab01/code :

– CS1001 Lecture 27 – 6
[’my_code2.py’, ’my_code1.py’]
Calling getSize on courses/comp1001/lab01/code/my_code2.py
File courses/comp1001/lab01/code/my_code2.py size= 47
Calling getSize on courses/comp1001/lab01/code/my_code1.py
File courses/comp1001/lab01/code/my_code1.py size= 37
Calling getSize on courses/comp1001/assign01
Directory courses/comp1001/assign01 :
[’data.dat’]
Calling getSize on courses/comp1001/assign01/data.dat
File courses/comp1001/assign01/data.dat size= 12
The size of courses is 226 bytes

– CS1001 Lecture 27 – 7
Recursion vs. Iteration

• Recursion is another form of flow control.

• Recursion is repetition without a loop.

• Any problem that can be solved recursively can be


solved using loops.

• Recursion has substantial overhead - it takes more


time and consumes more memory than iteration.

• When to use recursion?


– Use recursion when it allows you to specify a
clear, simple solution for a naturally recursive
problem that would be difficult to solve using
loops. For example, the directory-size problem or
drawing fractals.
– If an iterative solution is obvious, use
loops. Recursive solutions may be simple and
straightforward, but they are not efficient.

– CS1001 Lecture 27 – 8

You might also like