top of page

Learning Pathlib in Python

At Haruki Robotics Lab, we teach and learn by making. Even a small project like this helps students understand how coding can bridge the digital world with the physical one.


At Haruki Robotics Lab, students don’t just write programs — they also learn how to manage files and directories so projects stay organized. In this lesson, we look at the pathlib library and explore the exact methods our image‑downloader project uses.



ree

Python Code

from pathlib import Path

keyword_dir = Path("images") / "panda"
keyword_dir.mkdir(parents=True, exist_ok=True)

for file in keyword_dir.iterdir():
    print(file.suffix)
    new_name = keyword_dir / f"panda_1{file.suffix.lower()}"
    file.rename(new_name)

keyword_dir.rmdir()

How It Works

  • Path(): Creates a path object.

  • / operator: Joins paths neatly (e.g. Path("images") / "panda").

  • .mkdir(parents=True, exist_ok=True): Creates folders safely, even if parents are missing.

  • .iterdir(): Lists files and directories inside a folder.

  • .suffix: Gets the file extension (like .jpg).

  • .rename(new_name): Renames or moves a file.

  • .rmdir(): Removes a folder (only if it is empty).



1. Creating a Path

from pathlib import Path

p = Path("images") / "panda"
print(p)

The / operator joins directories together neatly. Instead of "images/" + "panda", you write Path("images") / "panda".

2. Making Folders

p.mkdir(parents=True, exist_ok=True)
  • parents=True → creates missing parent directories.

  • exist_ok=True → doesn’t crash if the folder already exists.

3. Listing Files

for item in p.iterdir():
    print(item.name)

.iterdir() loops through everything inside a folder and returns Path objects instead of strings.

4. Checking File Extensions

f = Path("photo.JPG")
print(f.suffix)        # ".JPG"
print(f.suffix.lower()) # ".jpg"

.suffix gives you the file extension, handy for filtering images only (.jpg, .png).

5. Renaming or Moving Files

old = Path("images/panda/raw/img1.jpg")
new = Path("images/panda/panda_1.jpg")
old.rename(new)

.rename() lets you change the file name or move it to another folder.

6. Removing Folders

Path("images/panda/raw").rmdir()

Remember: .rmdir() only works if the folder is empty. Otherwise, Python raises an error.


Practice Exercise

Try this mini‑program to understand the full flow:

from pathlib import Path

folder = Path("demo")
folder.mkdir(exist_ok=True)

# Create a dummy file
file = folder / "hello.txt"
file.write_text("Hello world!")

# Check what’s inside
for f in folder.iterdir():
    print(f, f.suffix)

# Rename file
file.rename(folder / "new_hello.txt")

# Remove file and folder
(folder / "new_hello.txt").unlink()
folder.rmdir()

Learning Goals

Students learn to manage files programmatically instead of manually. These skills are critical when handling images, logs, or sensor data in robotics projects.


Next Steps

  • Write a script that renames every image in a folder automatically.

  • Filter files by extension (e.g. only process .jpg).

  • Combine pathlib with an image crawler for fully automated downloads.


At Haruki Robotics Lab, we teach and learn by making. With pathlib, students see how a small set of methods keep their projects structured, reliable, and easy to scale.


Comments


bottom of page