-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_file_functions.py
More file actions
124 lines (107 loc) · 4.43 KB
/
text_file_functions.py
File metadata and controls
124 lines (107 loc) · 4.43 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Functions for reading and writing text files
"""
from pathlib import Path
import json
import logging
logger = logging.getLogger(__name__)
def read_text_file(file_path: Path | str) -> str:
"""
Reads a text file and returns its entire contents as a single string.
Includes error checking and logging.
Args:
file_path (Path | str): The file path of the text file to read.
Returns:
str: The entire contents of the text file as a single string.
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "r", encoding="utf-8") as f:
text = f.read()
logger.info(f"Successfully read {json.dumps(str(file_path))}")
return text
except FileNotFoundError:
logger.error(f"File not found: {json.dumps(str(file_path))}")
return ""
except PermissionError:
logger.error(f"Permission denied: {json.dumps(str(file_path))}")
return ""
except OSError as e:
logger.error(f"Error reading {json.dumps(str(file_path))}", e)
return ""
def read_text_file_lines(file_path: Path | str) -> list[str]:
"""
Reads a text file and returns a list of strings with the \n characters at the end of each line removed.
Includes error checking and logging.
Args:
file_path (Path | str): The file path of the text file to read.
Returns:
list[str]: A list of strings with the \n characters at the end of each line removed.
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "r", encoding="utf-8") as f:
text_lines = [line.rstrip("\n\r") for line in f]
logger.info(f"Successfully read {json.dumps(str(file_path))}")
return text_lines
except FileNotFoundError:
logger.error(f"File not found: {json.dumps(str(file_path))}")
return []
except PermissionError:
logger.error(f"Permission denied: {json.dumps(str(file_path))}")
return []
except OSError as e:
logger.error(f"Error reading {json.dumps(str(file_path))}", e)
return []
def write_text_file(file_path: Path | str, text: str) -> None:
"""
Writes a string to a text file. Includes error checking and logging.
Args:
file_path (Path | str): The file path of the text file to write.
text (str): A string to write to the text file.
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "w", encoding="utf-8") as f:
f.write(text)
logger.info(f"Successfully wrote {json.dumps(str(file_path))}")
except FileNotFoundError:
logger.error(f"File not found: {json.dumps(str(file_path))}")
except PermissionError:
logger.error(f"Permission denied: {json.dumps(str(file_path))}")
except OSError as e:
logger.error(f"Error writing {json.dumps(str(file_path))}", e)
def write_text_file_lines(file_path: Path | str, lines: list[str]) -> None:
"""
Writes a list of strings to a text file, with each string on a new line.
Includes error checking and logging.
Args:
file_path (Path | str): The file path of the text file to write.
lines (list[str]): A list of strings to write to the text file.
Returns:
None
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "w", encoding="utf-8") as f:
for line in lines:
f.write(line + "\n")
logger.info(f"Successfully wrote {json.dumps(str(file_path))}")
except FileNotFoundError:
logger.error(f"File not found: {json.dumps(str(file_path))}")
except PermissionError:
logger.error(f"Permission denied: {json.dumps(str(file_path))}")
except OSError as e:
logger.error(f"Error writing {json.dumps(str(file_path))}", e)