MCS-201 · Unit 11

Functions and File Handling in Python

Section 8 of 8

Block 3 · Unit 11Functions and File Handling in Python9 min · Section 8 of 8

11.8Copying, Deleting and Inspecting Files

Covers IGNOU MCS-201 §11.8.3, 11.8.4

Besides reading and writing contents, programs often need to manage files as a whole: check whether a file exists, find its size, copy, rename or delete it, and list a folder. The standard modules os, os.path and shutil provide these operations.

Useful functions

FunctionPurpose
os.path.exists(p)True if a file or folder p exists
os.path.isfile(p), os.path.isdir(p)True if p is an existing file / folder
os.path.getsize(p)Size of the file in bytes
os.stat(p)Detailed information: size (st_size), modification time and more
os.listdir(p)Names of the entries in folder p (in no particular order)
os.getcwd(), os.chdir(p)Get / change the current working directory
os.mkdir(p)Create a folder
os.rename(a, b)Rename or move a file
os.remove(p)Delete a file
os.rmdir(p)Delete an empty folder
shutil.copy(src, dst)Copy a file’s contents (and permissions)
shutil.rmtree(p)Delete a folder and everything in it (use with great care)

The newer pathlib module offers the same operations through Path objects, for example Path("notes.txt").exists() and Path("notes.txt").stat().st_size.

Key takeaways

  • os.path.exists, isfile, isdir and getsize inspect files.
  • shutil.copy copies; os.rename renames or moves; os.remove deletes a file.
  • os.rmdir removes only empty folders; shutil.rmtree removes a whole tree.
  • os.listdir lists a folder in arbitrary order; sort it if order matters.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which function copies a file?

Show answer

Answer: B. shutil.copy()

Copying whole files is provided by the shutil (“shell utilities”) module.

Question 2 · multiple choice

Which function deletes a file?

Show answer

Answer: A. os.remove()

os.remove() deletes a file; os.rmdir() deletes an empty folder.

Question 3 · multiple choice

A file contains exactly hello followed by a newline. What does os.path.getsize() return for it?

Show answer

Answer: C. 6

Five letters plus the newline character, one byte each (on Linux and macOS; Windows text mode would store the newline as two bytes).

Question 4 · multiple choice

What does this program print?

Python · quiz_os.py
import os

with open("tmp_a.txt", "w") as f:
    f.write("data")
print(os.path.exists("tmp_a.txt"), end=" ")
os.remove("tmp_a.txt")
print(os.path.exists("tmp_a.txt"))
Show answer

Answer: D. True False

The file exists after it is written and no longer exists after os.remove().

Question 5 · multiple choice

Why might os.rmdir("files") fail?

Show answer

Answer: C. The folder still contains files or subfolders

os.rmdir() removes only empty folders; use shutil.rmtree() (carefully) for a folder with contents.

Question 6 · short answer

Write a program that makes a backup copy of data.txt named data_backup.txt, but only if data.txt exists, and prints the size of the backup.

Show worked answer

import os, shutil, then if os.path.exists("data.txt"): shutil.copy("data.txt", "data_backup.txt") followed by print(os.path.getsize("data_backup.txt"), "bytes"), and else: print("data.txt not found"). Checking first avoids FileNotFoundError; shutil.copy copies the contents; getsize confirms the backup has the same size as the original.

↑ ↓ move↵ openTab switch type