write:
f = open('data.txt', 'w')
f.write('Hello\n')
f.write('world\n')
f.close()
read:
f = open('data.txt')
text = f.read()
print(text)
print( text.split())
#Hello
#World
#['Hello', 'world']
read in binary
data = open('data.txt', 'rb').read()
print(data) #b'Hello\r\nworld\r\n'
Operations
f.close(x) close file f. f.fileno(x) get fileno (fd) for f. f.flush(x) flush file's internal buffer. f.isatty() 1 if file is connected to a tty-like dev, else 0 f.read([size]) read at most most bytes from file and return as a string object. If omitted, read to EOF. f.readline() read one entire line from file f.readlines() read until EOF with readline() and return list of lines read. f.seek(offset, whence=0) set file's position, like "stdio's fseek()". whence == 0 then use absolute indexing whence == 1 then offset relative to current pos whence == 2 then offset relative to file end f.tell() return file's current position f.write(str) Write string to file. f.writelines(list) Write list of strings to file.