› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › How Djangoโs listdir() Separates Files and Folders
- This topic is empty.
-
AuthorPosts
-
March 24, 2026 at 3:50 pm #6267
When working on projects like the CS50 Wiki, you often come across this line:
_, filenames = default_storage.listdir("entries")At first glance, it feels like magic:
๐ค How does Django know which are files and which are folders?
๐ค Where are these two lists coming from?Letโs break it down step by step with proper context.
๐งฉ Initial Context (Real Use Case)
In the Wiki project, you store entries like this:
entries/ Python.md Django.md HTML.mdYour goal is:
๐ Get a list of all encyclopedia entry names (without
.md)So you write:
def list_entries(): _, filenames = default_storage.listdir("entries") return list(sorted( re.sub(r"\.md$", "", filename) for filename in filenames if filename.endswith(".md") ))
โ The Big Question
Where do these two lists come from?
_, filenames = default_storage.listdir("entries")Why not just one list?
๐ The Answer Lies Inside Django
This behavior is defined in Djangoโs internal class:
FileSystemStorage.listdir()๐ Located in:
django/core/files/storage.py
โ๏ธ Djangoโs Internal Logic (Simplified)
Hereโs a simplified version of what Django actually does:
def listdir(self, path): full_path = self.path(path) directories = [] files = [] for entry in os.listdir(full_path): full_entry_path = os.path.join(full_path, entry) if os.path.isdir(full_entry_path): directories.append(entry) else: files.append(entry) return directories, files
๐ง Step-by-Step Understanding
1. Django looks inside the folder
os.listdir(full_path)๐ Gets everything inside
"entries"
(both files and folders)
2. It creates two empty lists
directories = [] files = []
3. It checks each item
if os.path.isdir(full_entry_path):๐ Uses the OS to decide:
- Folder โ goes to
directories - File โ goes to
files
4. It returns BOTH lists
return directories, files
๐ Example
Suppose your folder looks like:
entries/ Python.md Django.md drafts/ notes.txtDjango will return:
(["drafts"], ["Python.md", "Django.md", "notes.txt"])
๐ How YOUR code uses it
_, filenames = default_storage.listdir("entries")This means:
_โ ignore folders (["drafts"])filenamesโ keep files (["Python.md", "Django.md", "notes.txt"])
๐ Final Filtering Happens Here
if filename.endswith(".md")๐ Keeps only:
["Python.md", "Django.md"]
๐ฏ Final Output
After removing
.md:["Django", "Python"]
โก Key Takeaways
- Django explicitly creates two lists:
- ๐ directories
- ๐ files
- It uses:
os.path.isdir()to decide where each item goes
- You control what to use:
_, filenames = ...
๐ก Simple Mental Model
Think of Django doing this:
๐งบ โHere are two baskets:
one for folders, one for files โ you choose what you want.โ
๐ Bonus Insight
Even if you switch to cloud storage (like AWS S3):
- Django still returns
(directories, files) - But internally it may simulate folders
- Folder โ goes to
-
AuthorPosts
- You must be logged in to reply to this topic.
