Skip to content

writeToFile(): append mode, CWD-relative output path, and unspecified encoding #11

Description

@dmccoystephenson

Three separate correctness problems live in writeToFile():

def writeToFile(self):
    # make folder if nonexistent
    if not os.path.exists("ideas"):
        os.makedirs("ideas")

    # get timestamp for filename
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
    filename = "ideas/ideas-" + timestamp + ".txt"

    # write idea to file
    with open(filename, "a") as f:
        for i in range(len(self.pairs)):
            f.write(str(self.pairs[i]) + ": " + self.ideas[i] + "\n")

1. The output path is relative to the caller's working directory

"ideas" is resolved against the process's CWD, not against the source file. Started from the repository root, output lands in the tracked ideas/ directory as intended; started from src/, a second src/ideas/ directory is silently created instead. Nothing in the repository documents this.

2. The file is opened in append mode for a name just made unique

The timestamp has one-second resolution, so "a" has exactly two effects: two runs completing within the same second silently merge into one file, and every other run appends to a file that was just created empty. "w" is what the surrounding code means.

3. No encoding is specified

open() without encoding= uses the platform default, so a keyword or idea containing non-ASCII text can raise UnicodeEncodeError on a machine whose default encoding is not UTF-8, and files written on one platform can be misread on another.

Suggested resolution

These three sit in the same six-line method and form a natural single change: pass "w", pass encoding="utf-8", and either resolve the output directory relative to the source file or document the CWD requirement in README.md. The written line format (str(pair) + ": " + idea) and the ideas/ideas-<timestamp>.txt filename must not change — ideas/example.txt records that contract.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions