Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

os
python3
communitycreator

How to list directory in Python 3

Dario Vincenzo Tarantini

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 os

open("/tmp/test.txt", 'a') # lets create a blank file

arr = os.listdir("/tmp")
print(arr)

It needs only one parameter, the path we want to list.

Using glob.glob()

import glob

open("/tmp/test.txt", 'a') # lets create a blank file

arr = glob.glob("/tmp/*.txt")
print(arr)

Its use is basically the same as listdir.

Note: glob.glob() also accepts wildcards.

RELATED TAGS

os
python3
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini
RELATED COURSES

View all Courses

Keep Exploring