Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/files/test_file_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def test_file_methods():
# argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current
# file position, and 2 uses the end of the file as the reference point. from_what can be omitted
# and defaults to 0, using the beginning of the file as the reference point.
assert binary_file.seek(0) == 0 # Go to the 0th byte in the file
assert binary_file.seek(5) == 5 # Go to the 0th byte in the file
assert binary_file.seek(6) == 6 # Go to the 6th byte in the file

assert binary_file.read(1) == '6'
assert binary_file.read(4) == '6789'

# f.readline() reads a single line from the file; a newline character (\n) is left at the end
# of the string, and is only omitted on the last line of the file if the file doesn’t end in a
Expand Down
17 changes: 17 additions & 0 deletions src/files/test_file_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ def test_files_open():

assert file.closed

with open('src/files/write_file.txt', 'w') as file:
file.write('apple\n')
file.write('banana\n')

with open('src/files/write_file.txt', 'a') as file:
file.write('pineapple\n')


with open('src/files/write_file.txt', 'r') as file:
read_data = file.read()

assert read_data == (
'apple\n'
'banana\n'
'pineapple\n'
)

# If you’re not using the with keyword, then you should call f.close() to close the file and
# immediately free up any system resources used by it. If you don’t explicitly close a file,
# Python’s garbage collector will eventually destroy the object and close the open file for you,
Expand Down
3 changes: 3 additions & 0 deletions src/files/write_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apple
banana
pineapple