Source code for mdutils.fileutils.fileutils

# Python
#
# This module implements a main class that allows to create markdown files, write in them or read.
#
# This file is part of mdutils. https://github.com/didix21/mdutils
#
# MIT License: (C) 2018 Dídac Coll
from typing import Optional


[docs]class MarkDownFile(object): """MarkDownFile class creates a new file of MarkDown extension. Features available are: - Create a file. - Rewrite a file with new data. - Write at the end of the file.""" def __init__(self, name="", dirname: Optional[str] = None): """Creates a markdown file, if name is not empty. :param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`. :param str dirname: use dirname if you want to provide the path separately from the name. If not you can specify the path directly on the name.""" if name: self.dirname = dirname self.file_name = self._get_file_name(name, dirname) self.file = open(f"{self.file_name}", "w+", encoding="UTF-8") self.file.close() def _get_file_name(self, name: str, dirname: Optional[str] = None) -> str: if dirname: return ( f"{dirname}/{name}" if name.endswith(".md") else f"{dirname}/{name}.md" ) return name if name.endswith(".md") else f"{name}.md"
[docs] def rewrite_all_file(self, data: str): """Rewrite all the data of a Markdown file by ``data``. :param str data: is a string containing all the data that is written in the markdown file. """ with open(f"{self.file_name}", "w", encoding="utf-8") as self.file: self.file.write(data)
[docs] def append_end(self, data: str): """Write at the last position of a Markdown file. :param str data: is a string containing all the data that is written in the markdown file. """ with open(f"{self.file_name}", "a", encoding="utf-8") as self.file: self.file.write(data)
[docs] def append_after_second_line(self, data: str): """Write after the file's first line. :param str data: is a string containing all the data that is written in the markdown file. """ with open(f"{self.file_name}", "r+", encoding="utf-8") as self.file: file_data = self.file.read() # Save all the file's content self.file.seek(0, 0) # Place file pointer at the beginning first_line = self.file.readline() # Read the first line second_line = self.file.readline() # Read the second line self.file.seek( len(first_line + second_line), 0 ) # Place file pointer at the end of the first line self.file.write(data) # Write data self.file.write("\n" + file_data[len(first_line + second_line):])
[docs] @staticmethod def read_file(file_name: str) -> str: """Read a Markdown file using a file name. It is not necessary to add *.md extension. :param file_name: Markdown file's name. :type file_name: str :return: return all file's data. :rtype: str""" if file_name.find(".md") == -1: file_name += ".md" with open(file_name, "r", encoding="utf-8") as file: file_data = file.read() return file_data
if __name__ == "__main__": new_file = MarkDownFile("Example") new_file.rewrite_all_file(data="# Some Text Example")