11import os
22
33class File :
4- def __init__ (self , filePath ):
5- self .__m_filePath = filePath
6- self .__m_dirPath = os .path .dirname (self .__m_filePath )
7- if os .path .exists (self .__m_dirPath ) == False : # If the directory of the file path does not exist, it will be created.
8- os .mkdir (self .__m_dirPath )
4+ def __init__ (self , file_path ):
5+ self .__m_file_path = file_path
6+ self .__m_file_full_name = os .path .basename (self .__m_file_path )
7+ self .__m_file_name , self .__m_file_extension = os .path .splitext (self .__m_file_full_name )
8+ self .__m_dir_path = os .path .dirname (self .__m_file_path )
9+
10+ if os .path .exists (self .__m_dir_path ) == False : # If the directory of the file path does not exist, it will be created.
11+ os .mkdir (self .__m_dir_path )
912 try : # If the file does not exist, it will be created.
10- self .__m_file = open (self .__m_filePath , "x" )
11- except :
12- pass
13+ self .__m_file = open (self .__m_file_path , "x" )
14+ except FileExistsError :
15+ pass # Indicates that the file exists and no further exception handling is required.
1316 else :
1417 self .__m_file .close ()
1518
1619 @property
1720 def content (self ):
18- self .__m_file = open (self .__m_filePath , "r" )
21+ self .__m_file = open (self .__m_file_path , "r" )
1922 self .__m_content = self .__m_file .read ()
2023 self .__m_file .close ()
2124 return self .__m_content
2225
23- def Rewrite (self ,content ):
24- self .__m_file = open (self .__m_filePath , "w" )
26+ @property
27+ def full_name (self ):
28+ return self .__m_file_full_name
29+
30+ @property
31+ def name (self ):
32+ return self .__m_file_name
33+
34+ @property
35+ def ext (self ):
36+ return self .__m_file_extension .split ("." )[- 1 ]
37+
38+ def rewrite (self ,content ):
39+ self .__m_file = open (self .__m_file_path , "w" )
2540 self .__m_file .write (content )
2641 self .__m_file .close ()
2742
28- def Append (self ,content ):
29- self .__m_file = open (self .__m_filePath , "a" )
43+ def append (self ,content ):
44+ self .__m_file = open (self .__m_file_path , "a" )
3045 self .__m_file .write (content )
3146 self .__m_file .close ()
3247
33- def Delete (self ):
34- if os .path .exists (self .__m_filePath ):
35- os .remove (self .__m_filePath )
48+ def delete (self ):
49+ if os .path .exists (self .__m_file_path ):
50+ os .remove (self .__m_file_path )
0 commit comments