-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_constructor.py
More file actions
49 lines (41 loc) · 1.56 KB
/
os_constructor.py
File metadata and controls
49 lines (41 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
def main():
try:
# Get current working directory
current_dir = os.getcwd()
print(f"Current working directory: {current_dir}")
# List directory contents
dir_contents = os.listdir()
print("Directory contents:", dir_contents)
# Create a new directory if it doesn't exist
new_dir_name = "new_directory"
if not os.path.exists(new_dir_name):
os.makedirs(new_dir_name)
print(f"Created new directory: {new_dir_name}")
else:
print(f"Directory '{new_dir_name}' already exists")
# Change the current working directory
try:
os.chdir(new_dir_name)
print(f"Changed to directory: {os.getcwd()}")
except PermissionError:
print(f"Permission denied when trying to change to {new_dir_name}")
# Example of file creation (safely)
file_name = "example.txt"
with open(file_name, "w") as file:
file.write("This is a test file.")
print(f"Created file: {file_name}")
# Safely remove the file we just created
if os.path.exists(file_name):
os.remove(file_name)
print(f"Removed file: {file_name}")
else:
print(f"File {file_name} does not exist to remove.")
except FileNotFoundError as e:
print(f"File or directory not found: {e}")
except PermissionError as e:
print(f"Permission error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()