-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwriter.py
More file actions
44 lines (39 loc) · 1.27 KB
/
Copy pathwriter.py
File metadata and controls
44 lines (39 loc) · 1.27 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
def write_file(filename: str, text: str):
try:
with open(filename, "w") as f:
f.write(text)
except IOError as e:
print(f"Error writing to file {filename}: {e}")
except Exception as e:
print(f"Unexpected error occurred: {e}")
def read_file(filename: str) -> str:
try:
with open(filename, "r") as f:
return f.read()
except FileNotFoundError:
print(f"File {filename} not found")
return ""
except IOError as e:
print(f"Error reading file {filename}: {e}")
return ""
except Exception as e:
print(f"Unexpected error occurred: {e}")
return ""
def append_file(filename: str, text: str):
try:
with open(filename, "a") as f:
f.write(text)
except IOError as e:
print(f"Error appending to file {filename}: {e}")
except Exception as e:
print(f"Unexpected error occurred: {e}")
def parse_input(text: str) -> str:
return text.strip('\n')
if __name__ == "__main__":
# Get the input text from the user and store it in a file
while True:
input_text = input("Enter your text: ")
append_file("output.txt", input_text)
if input_text == "":
break
print("File written successfully.")