-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_in_out.py
34 lines (27 loc) · 902 Bytes
/
file_in_out.py
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
"""
Demonstrate file input and output
"""
# First way - open the text file read-only
file = open('c:/temp/t.txt', 'rt')
# read the entire thing into RAM
file_lines: list = file.readlines()
file.close()
for line in file_lines:
print(line)
# second way does not read the entire file into RAM
# and automatically closes it when done reading it.
with open('c:/temp/t.txt', 'rt') as file:
for line in file:
print(line, end='')
FILE_BEGIN: int = 0
FILE_CURRENT: int = 1
FILE_END: int = 2
# binary files
with open('c:/temp/t.bin', mode='rb') as file:
file.seek(1, 0) # move forward one byte from the beginning
print(file.tell()) # prints 1
file.seek(-10, 2) # move ten bytes back from the end
# python mmap (memory mapped files)
# https://docs.python.org/3/library/mmap.html
# __str__() vs __repr__() / str() vs repr()
# https://www.geeksforgeeks.org/str-vs-repr-in-python/