Member-only story
10 Python Os/Sys Things I Regret Not Knowing Earlier
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…