-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path12_file_viewer.py
More file actions
55 lines (44 loc) · 1.63 KB
/
Copy path12_file_viewer.py
File metadata and controls
55 lines (44 loc) · 1.63 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
50
51
52
53
54
55
# made by @ericwasepic127 (Fixed)
import os
import sys
def view_file(location):
if not os.path.isfile(location):
print(f"ERROR: File '{location}' doesn't exist or it's a directory.")
return
try:
# Added utf-8 encoding to prevent crash on special characters
with open(location, mode='r', encoding='utf-8') as file:
print("File:", location)
print("-" * 40)
print(file.read())
except PermissionError:
print(f"ERROR: Failed to read file '{location}': Operation not permitted")
except UnicodeDecodeError:
print(f"ERROR: Cannot read '{location}'. It might be a binary file (like an image or executable) or use a non-UTF-8 encoding.")
except Exception as e:
print("ERROR:", e)
print("Welcome to File viewer!")
print("Use 'exit' or Ctrl-C to exit")
print("You're at", os.getcwd(), "directory!")
print("In your folder, it has ...")
try:
print("- " + "\n- ".join(os.listdir()))
except Exception as e:
print("- (Unable to list directory contents)")
# FIX: Initialize the variable so the while loop doesn't crash on start
location = ""
while location != "exit":
try:
location = input("\nEnter path to your file to view: ").strip()
if not location:
print("No location entered to view!")
continue
if location != "exit":
view_file(location)
# FIX: Added the missing colon
except KeyboardInterrupt:
print() # Clean newline after Ctrl+C
break
except Exception as e:
print("ERROR:", e)
print("Exit received! Bye!")