Learning Python(7) Files


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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值