How to list directory in Python 3
Sometimes, we need to list all directories in a certain path. This simple task can be done in a few different ways:
Using os.listdir()
import osopen("/tmp/test.txt", 'a') # lets create a blank filearr = os.listdir("/tmp")print(arr)
It needs only one parameter, the path we want to list.
Using glob.glob()
import globopen("/tmp/test.txt", 'a') # lets create a blank filearr = glob.glob("/tmp/*.txt")print(arr)
Its use is basically the same as listdir.
Note:
glob.glob()also accepts wildcards.