-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_2.py
More file actions
32 lines (24 loc) · 1 KB
/
Copy pathExercise_2.py
File metadata and controls
32 lines (24 loc) · 1 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
import os
import logging
# Configure logging
logging.basicConfig(filename='file_errors_log.txt', level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s')
def read_file():
try:
file_path = input("Enter the path of the file you want to read: ")
if not os.path.isfile(file_path):
raise FileNotFoundError(f"File '{file_path}' does not exist.")
with open(file_path, 'r') as file:
content = file.read()
print("File Content:\n", content)
except FileNotFoundError as fnf_error:
logging.error(fnf_error)
print(f"Error: {fnf_error}")
except PermissionError:
logging.error(f"Permission denied for file: {file_path}")
print("Error: You don't have permission to access this file.")
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
read_file()