OS Library Flashcards
Get the current working directory
os.getcwd()
Change the working directory
os.chdir(“C:\Users\Sarper\Desktop”)
List all entries in a directory
os.listdir(path=”C:\Users\Sarper\Downloads”)
Create a directory
os.chdir(“C:\Users\Sarper\Downloads”)
print(os.listdir())
os.mkdir(“New file”)
print(os.listdir())
Join path components
path = os.path.join(‘path1’, ‘path2’, “file.txt”)
Check if a path exists
os.path.exists(path)
Check if path is a file or a directory
os.path.isfile(“C:\Users\Sarper\Downloads\projects”)
os.path.isdir(“C:\Users\Sarper\Downloads\projects”)
os.path.isfile(“C:\Users\Sarper\Downloads\Picture1.png”)
os.path.isdir(“C:\Users\Sarper\Downloads\Picture1.png”)
Run system command
os.system(r”cd”)
Delete a file
[‘Picture1.png’, ‘Picture2.png’, ‘projects’]
import os
os.chdir(r”C:\Users\Sarper\Downloads”)
print(os.listdir())
os.remove(“Picture1.png”)
print(os.listdir())
# [‘Picture3.png’, ‘projects’]
Rename a file or a directory
os.rename(r”C:\Users\Sarper\Downloads\Picture2.png”, r”C:\Users\Sarper\Downloads\Picture3.png”)
Delete a directory
os.rmdir(path)