Python os Module


Python has a built-in os module with methods for interacting with the operating system. You can create files and directories for example.

import os
# get the current directory
os.getcwd()

# change the directory to an existing directory
os.chdir('D:\\Test')
os.getcwd()  # get the new current directory

# make a new directory in the current working directory
os.makedirs('new_dir')

os.listdir('D:\\Test\\new_dir')

os.path.exists('D:\\A5Gt9Hy2bm')

Returns False.

os.path.exists('D:\\Test\\new_dir')

Returns True.


Leave a Reply