File: io.py

package info (click to toggle)
django-maintenance-mode 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: python: 499; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 674 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-

import os


def read_file(file_path, default_content=''):
    """
    Read file at the specified path.
    If file doesn't exist, it will be created with default-content.
    Returns the file content.
    """
    if not os.path.exists(file_path):
        write_file(file_path, default_content)

    handler = open(file_path, 'r')
    content = handler.read()
    handler.close()
    return content or default_content


def write_file(file_path, content):
    """
    Write file at the specified path with content.
    If file exists, it will be overwritten.
    """
    handler = open(file_path, 'w+')
    handler.write(content)
    handler.close()