1+ import os
2+
3+ def create_file (filename ):
4+ try :
5+ with open (filename , 'x' ) as f :
6+ print (f"File name { filename } : Created successfully!" )
7+ except FileExistsError :
8+ print (f'File name { filename } already exists!' )
9+ except Exception as e :
10+ print ('An error occurred!' )
11+
12+ def view_all_file ():
13+ files = os .listdir ()
14+ if not files :
15+ print ('No file found!' )
16+ else :
17+ print ('Files in directory:' )
18+ for file in files :
19+ print (file )
20+
21+ def delete_file (filename ):
22+ try :
23+ os .remove (filename )
24+ print (f'{ filename } has been deleted successfully!' )
25+ except FileNotFoundError :
26+ print ('File not found!' )
27+ except Exception as e :
28+ print ('An error occurred!' )
29+
30+ def read_file (filename ):
31+ try :
32+ with open (filename , 'r' ) as f :
33+ content = f .read ()
34+ print (f"Content of '{ filename } ':\n { content } " )
35+ except FileNotFoundError :
36+ print (f"{ filename } doesn't exist!" )
37+ except Exception as e :
38+ print ('An error occurred!' )
39+
40+ def edit_file (filename ):
41+ try :
42+ with open (filename ,'a' ) as f :
43+ content = input ("Enter data to add = " )
44+ f .write (content + "\n " )
45+ print (f'Content added to { filename } Successfully!' )
46+ except FileNotFoundError :
47+ print (f"{ filename } doesn't exist!" )
48+ except Exception as e :
49+ print ('An error occurred!' )
50+
51+ def main ():
52+ while True :
53+ print ("\n FILE MANAGEMENT APP" )
54+ print ('1: Create file' )
55+ print ('2: View all files' )
56+ print ('3: Delete file' )
57+ print ('4: Read file' )
58+ print ('5: Edit file' )
59+ print ('6: Exit' )
60+
61+ choice = input ('Enter your choice (1-6) = ' )
62+
63+ if choice == '1' :
64+ filename = input ("Enter file name to create = " )
65+ create_file (filename )
66+
67+ elif choice == '2' :
68+ view_all_file ()
69+
70+ elif choice == '3' :
71+ filename = input ("Enter file name you want to delete = " )
72+ delete_file (filename )
73+
74+ elif choice == '4' :
75+ filename = input ("Enter file name to read = " )
76+ read_file (filename )
77+
78+ elif choice == '5' :
79+ filename = input ("Enter file name to edit = " )
80+ edit_file (filename )
81+
82+ elif choice == '6' :
83+ print ('Closing the app....' )
84+ break
85+
86+ else :
87+ print ('Invalid choice!' )
88+
89+ if __name__ == "__main__" :
90+ main ()
0 commit comments