Skip to content

set_key does not check if there is a carriage return before inserting new key #360

@sperretcraft

Description

@sperretcraft

Example script :

import os
import dotenv

DOTENV_FILE = os.path.join(os.path.dirname(__file__), ".env")

def init_dotenv():
    with open(DOTENV_FILE, "w") as fp:
        fp.write("TEST=ABCD") 


def print_dotenv():
    with open(DOTENV_FILE) as fp:
        content = fp.read()
    print("-" * 30, "DOTENV", "-" * 30)
    print(content)
    print()

init_dotenv()
print_dotenv()
dotenv.set_key(DOTENV_FILE, "FOO", "1234")
print_dotenv()

Output

------------------------------ DOTENV ------------------------------
TEST=ABCD

------------------------------ DOTENV ------------------------------
TEST=ABCDFOO='1234'


Is this something you want to fix ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions