-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathout_file.py
More file actions
73 lines (51 loc) · 2.22 KB
/
out_file.py
File metadata and controls
73 lines (51 loc) · 2.22 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
#? Writing into a file
#* with open("assets/out_file.txt", "w") as output_file:
#* What does "w" mean?
# The "w" mode in the `open()` function stands for "write."
"""
The "w" mode is used to write data to a file.
This mode is typically used when you want to create a new file
or overwrite an existing file with new data.
If you want to append data to an existing file
without truncating it, you can use the "a" mode (append) instead.
"""
# Use Case 1: Writing a single line to a file
with open("assets/out_file.txt", "w") as output_file:
output_file.write("This is a new session again.\n") # Write a single line to the file
# The "\n" at the end adds a newline character to separate lines in the file.
"""
The `writelines()` method takes a list of strings and writes them to
the file without adding newline characters automatically.
You need to include the newline characters in the strings
if you want them to appear on separate lines in the file.
"""
with open("assets/out_file.txt", "w") as output_file:
lines_to_write = [
"This is the first line.\n",
"This is the second line.\n",
"This is the third line.\n"
]
output_file.writelines(lines_to_write) # Write multiple lines to the file
# Example 2: Writing multiple lines to a file
#? Example 3: Appending to a file
with open("assets/out_file.txt", "a") as output_file:
output_file.write("This line will be appended to the file.\n") # Append a line to the file
"""
The "a" mode is used to open the file for appending,
which means new content will be
added to the end of the file without truncating it.
This is useful when you want to add new data to an
existing file without losing the previous content.
"""
# Use append "a" mode to append4 lines the file
with open("assets/out_file.txt", "a") as output_file:
lines_to_append = [
"This is the first appended line.\n",
"This is the second appended line.\n",
"This is the third appended line.\n"
"0: Project Time : 28th April 2025\n",
"1: Student Ipek ID: 353637474\n",
"1: Student Udua ID: 353637474\n",
"2: Student Chima ID: 353637474\n",
]
output_file.writelines(lines_to_append) # Append multiple lines to the file