os/path
code: python
import os
def remove_directory(path):
try:
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
remove_directory(file_path)
os.rmdir(path)
print(f"Successfully removed directory: {path}")
except Exception as e:
print(f"Error removing directory: {e}")
try:
current_folder = os.getcwd()
print(current_folder)
print(os.path.isdir(current_folder))
print(os.path.exists(os.path.join(current_folder, "foo.txt")))
os.mkdir('newdir')
print(os.listdir(current_folder))
os.chdir('newdir')
with open('bar.txt', 'w') as f:
f.write('hello')
with open('bar.txt', 'r') as f:
print(f.readline())
os.mkdir('newdir2')
os.chdir('../')
remove_directory('newdir')
except Exception as e:
print(f"Unexpected error: {e}")