Member-only story

10 Python Os/Sys Things I Regret Not Knowing Earlier

Liu Zuo Lin
6 min readNov 9, 2024

--

Read Free: https://zlliu.medium.com/10-python-os-sys-things-i-regret-not-knowing-earlier-468c69962920?sk=35a89bc1a0a21a45a6cb0baf34c3fb52

1) os.listdir

This is the first os function I learnt. It takes in a folder name, and returns a list of strings representing stuff inside our folder.

Let’s say we have a folder named blank containing a bunch of text files. Note that the folder named blank is in the same folder as our Python script.

And if we run the code below, we get a list of strings. This list of strings represent the stuff that is found inside the blank folder.

Note — listing is done only at one level, and not recursively. In simple words, if we have subfolders, the contents of the subfolders won’t be included.

Let’s create more subfolders with stuff inside.

And if we run the same Python script, notice that os.listdir only lists subfolder2 & subfolder1

--

--

Liu Zuo Lin
Liu Zuo Lin

Written by Liu Zuo Lin

SWE @ Meta | [Ebook] 101 Things I Never Knew About Python: https://payhip.com/b/vywcf

Responses (11)