88. Python's os and shutil Modules
1. Creating Directories with os.makedirs
import os
# Create a directory along with intermediate directories
directory_path = 'parent_folder/child_folder'
os.makedirs(directory_path, exist_ok=True)
print(f"Directory created: {directory_path}")2. Renaming a File with os.rename
import os
# Rename a file
old_name = 'old_file.txt'
new_name = 'new_file.txt'
os.rename(old_name, new_name)
print(f"File renamed from {old_name} to {new_name}")3. Removing a File with os.remove
4. Listing Files in a Directory with os.listdir
5. Copying a File with shutil.copy
6. Moving a File with shutil.move
7. Removing an Empty Directory with os.rmdir
8. Deleting a Directory and Its Contents with shutil.rmtree
9. Getting File Information with os.stat
10. Changing File Permissions with os.chmod
Last updated