os.listdir returns a list of filenames and folder names inside a directory. With a relative path, Python resolves that path from the current working directory. section 12.2.2 section 12.2.1
Use os.listdir with these practical rules:
- Pass os.curdir to list the current working directory portably instead of writing a literal dot. section 12.2.2 section 12.2.6
- The result contains directory entries but does not include the os.curdir or os.pardir indicators. section 12.4
- After os.chdir changes the working directory, the same relative listing refers to the new location. section 12.2.2
- Use glob when you need shell-style wildcard filtering such as *.json, ?.tmp, or [0-9].tmp. section 12.4
List the current directory
import os os.listdir(os.curdir) # ['.config', 'sample_data'] os.chdir('sample_data') os.listdir(os.curdir) # ['anscombe.json', 'README.md', 'california_housing_train.csv', ...] section 12.2.2
os.chdir('/content/sample_data')
os.listdir(os.curdir)
['anscombe.json',
'README.md',
'california_housing_train.csv',
'california_housing_test.csv',
'mnist_test.csv',
'mnist_train_small.csv']
Inspect a concrete listing of files in a changed working directory. section 12.4


