Header Ads

Responsive Ads Here

OS Module in Python

OS Module in Python

NOTE: Please change the path with the path of the folder you are working with.


import os
import shutil
print(os.getcwd())
os.mkdir("important")
print(os.path.exists("my_importants"))

if os.path.exists("my_importants"):
    print("The folder/file already exists...")
else:
    os.mkdir("my_importants")

open("file.txt","w").close()

os.chdir("D:\Python")

print(os.listdir("D:\Python"))

for f in os.listdir():
    path = os.path.join(os.getcwd(),f)
    print(path)

print(os.listdir("D:\python_tut"))

initial_folder = os.walk("D:\python_tut")

for path, folder, file in initial_folder:
    print(f"current path : {path}")
    print(f"folder : {folder}")
    print(f"file : {file}")

os.rmdir(r"D:\python_tut\empty_folder")
os.chdir("D:\python_tut")

os.makedirs("new_folder/music")

shutil.rmtree("D:\python_tut")

Powered by Blogger.