@@ -15,35 +15,42 @@ class MarkDownFile(object):
1515 - Rewrite a file with new data.
1616 - Write at the end of the file."""
1717
18- def __init__ (self , name = '' , tmp = '' ):
18+ def __init__ (self , name = '' , dirname : str = None ):
1919 """Creates a markdown file, if name is not empty.
20- :param str name: file name"""
21- import os
20+ :param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`.
21+ :param str dirname: use dirname if you want to provide the path separately from the name.
22+ If not you can specify the path directly on the name."""
2223 if name :
23- self .dirname = tmp if tmp else os . getcwd ()
24- self .file_name = name if name . endswith ( '.md' ) else name + '.md'
25- self .file = open (f'{ self .dirname } / { self . file_name } ' , 'w+' , encoding = 'UTF-8' )
24+ self .dirname = dirname
25+ self .file_name = self . _get_file_name ( name , dirname )
26+ self .file = open (f'{ self .file_name } ' , 'w+' , encoding = 'UTF-8' )
2627 self .file .close ()
2728
29+ def _get_file_name (self , name : str , dirname : str = None ) -> str :
30+ if dirname :
31+ return f'{ dirname } /{ name } ' if name .endswith ('.md' ) else f'{ dirname } /{ name } .md'
32+
33+ return name if name .endswith ('.md' ) else f'{ name } .md'
34+
2835 def rewrite_all_file (self , data ):
2936 """Rewrite all the data of a Markdown file by ``data``.
3037
3138 :param str data: is a string containing all the data that is written in the markdown file."""
32- with open (f'{ self .dirname } / { self . file_name } ' , 'w' , encoding = 'utf-8' ) as self .file :
39+ with open (f'{ self .file_name } ' , 'w' , encoding = 'utf-8' ) as self .file :
3340 self .file .write (data )
3441
3542 def append_end (self , data ):
3643 """Write at the last position of a Markdown file.
3744
3845 :param str data: is a string containing all the data that is written in the markdown file."""
39- with open (f'{ self .dirname } / { self . file_name } ' , 'a' , encoding = 'utf-8' ) as self .file :
46+ with open (f'{ self .file_name } ' , 'a' , encoding = 'utf-8' ) as self .file :
4047 self .file .write (data )
4148
4249 def append_after_second_line (self , data ):
4350 """Write after the file's first line.
4451
4552 :param str data: is a string containing all the data that is written in the markdown file."""
46- with open (f'{ self .dirname } / { self . file_name } ' , 'r+' , encoding = 'utf-8' ) as self .file :
53+ with open (f'{ self .file_name } ' , 'r+' , encoding = 'utf-8' ) as self .file :
4754 file_data = self .file .read () # Save all the file's content
4855 self .file .seek (0 , 0 ) # Place file pointer at the beginning
4956 first_line = self .file .readline () # Read the first line
@@ -53,7 +60,7 @@ def append_after_second_line(self, data):
5360 self .file .write ('\n ' + file_data [len (first_line + second_line ):])
5461
5562 @staticmethod
56- def read_file (file_name ) :
63+ def read_file (file_name : str ) -> str :
5764 """Read a Markdown file using a file name. It is not necessary to add *.md extension.
5865
5966 :param file_name: Markdown file's name.
0 commit comments