Skip to content

Commit

Permalink
Use idiomatic pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Sep 3, 2022
1 parent 710407e commit e8acd1b
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
affecting existing content.
"""


import os
from pathlib import Path


def append_to_newsfile(
Expand All @@ -22,7 +21,7 @@ def append_to_newsfile(
if *single_file* is True, add it to an existing file, otherwise create a
fresh one.
"""
news_file = os.path.join(directory, filename)
news_file = Path(directory) / filename

header, prev_body = _figure_out_existing_content(
news_file, start_string, single_file
Expand All @@ -34,7 +33,7 @@ def append_to_newsfile(
# Leave newlines alone. This probably leads to inconsistent newlines,
# because we've loaded existing content with universal newlines, but that's
# the original behavior.
with open(news_file, "w", encoding="utf8", newline="") as f:
with news_file.open("w", encoding="utf8", newline="") as f:
if header:
f.write(header)

Expand All @@ -53,14 +52,14 @@ def _figure_out_existing_content(news_file, start_string, single_file):
Empty file and per-release files have neither.
"""
if not single_file or not os.path.exists(news_file):
if not single_file or not news_file.exists():
# Per-release news files always start empty.
# Non-existent files have no existing content.
return "", ""

# If we didn't use universal newlines here, we wouldn't find *start_string*
# which usually contains a `\n`.
with open(news_file, encoding="utf8") as f:
with news_file.open(encoding="utf8") as f:
content = f.read()

t = content.split(start_string, 1)
Expand Down

0 comments on commit e8acd1b

Please sign in to comment.