1
+ #ViewMate File Reader
2
+ #supports txt,lrc,csv,dat,py
3
+ import termcolor
4
+ from termcolor import colored
5
+ import os
6
+ import pickle
7
+ import csv
8
+
9
+ def load_txt (path ):
10
+ try :
11
+ with open (path ,"r" ) as file :
12
+ print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
13
+ for line in file :
14
+ print (line .strip ())
15
+ except FileNotFoundError :
16
+ print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
17
+ except Exception as e :
18
+ print (f"an error occured: { e } " )
19
+
20
+ def load_lrc (path ):
21
+ try :
22
+ with open (path ,"r" ,encoding = "utf-8" ) as file :
23
+ print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
24
+ for line in file :
25
+ print (line .strip ())
26
+ except FileNotFoundError :
27
+ print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
28
+ except Exception as e :
29
+ print (f"an error occured: { e } " )
30
+
31
+ def load_csv (path ):
32
+ try :
33
+ with open (path ,"r" ) as file :
34
+ f = csv .reader (file )
35
+ print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
36
+ for row in f :
37
+ print (row )
38
+ except FileNotFoundError :
39
+ print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
40
+ except Exception as e :
41
+ print (f"an error occured: { e } " )
42
+
43
+ def load_py (path ):
44
+ try :
45
+ with open (path ,"r" ,encoding = "utf-8" ) as file :
46
+ contents = file .read ()
47
+ print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
48
+ print (contents )
49
+ except FileNotFoundError :
50
+ print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
51
+ except Exception as e :
52
+ print (f"an error occured: { e } " )
53
+
54
+ def load_dat (path ):
55
+ try :
56
+ with open (path ,"rb" ) as file :
57
+ print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
58
+ try :
59
+ data = pickle .load (file )
60
+ print (colored ("Pickle File loaded sucessfully" ,"green" ,attrs = ["bold" ]))
61
+ if isinstance (data ,(list ,tuple )):
62
+ for item in data :
63
+ print (item )
64
+ else :
65
+ print (data )
66
+ except pickle .UnpicklingError :
67
+ file .seek (0 )
68
+ data = file .read ()
69
+ print (colored (f"binary file loaded sucessfully { len (data )} bytes" ,"green" ,attrs = ["bold" ]))
70
+ print (data )
71
+ print ()
72
+ print (colored ("trying to decode text..." ,"yellow" ,attrs = ["bold" ]))
73
+ try :
74
+ text = data .decode ("utf-8" )
75
+ print (colored (f"decided text: \n { text } " ,"yellow" ))
76
+ except UnicodeDecodeError :
77
+ print (colored ("File is not valid utf-8 text!" ,"red" ,attrs = ["bold" ]))
78
+ except FileNotFoundError :
79
+ print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
80
+ except Exception as e :
81
+ print (f"an error occured: { e } " )
82
+
83
+ def load (path ):
84
+ parts = path .split ("." )
85
+ if len (parts ) > 1 :
86
+ format = parts [- 1 ].lower ()
87
+ else :
88
+ format = ""
89
+ if format == "txt" :
90
+ load_txt (path )
91
+ elif format == "lrc" :
92
+ load_lrc (path )
93
+ elif format == "csv" :
94
+ load_csv (path )
95
+ elif format == "dat" :
96
+ load_dat (path )
97
+ elif format == "py" :
98
+ load_py (path )
99
+ else :
100
+ print (colored ("Unsupported File Format!" ,"red" ,attrs = ["bold" ]))
101
+
102
+ file_path = input (colored ("Enter File Path: " ,"yellow" ,attrs = ["bold" ]))
103
+ print ()
104
+ load (file_path )
0 commit comments