Learning Python(7) Files

来源:互联网 发布:天谕捏脸数据玉虚男 编辑:程序博客网 时间:2024/06/05 22:52


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 0f.read([size]) read at most most  bytes from file andreturn as a string object. If  omitted,read to EOF.f.readline()   read one entire line from filef.readlines()  read until EOF with readline() and return listof lines read.f.seek(offset, whence=0)     set file's position, like "stdio's fseek()". whence == 0 then use absolute indexingwhence == 1 then offset relative to current poswhence == 2 then offset relative to file endf.tell()     return file's current positionf.write(str)Write string to file.f.writelines(list)Write list of strings to file.

原创粉丝点击