打开—操作–关闭
f = open(’/home/kiosk/file’) ##读,默认就是读的(打开),读的文件必须存在
# print (f)
countent = f.read ##操作
print(countent)
f.close() ##关闭
f = open('/home/kiosk/file','w') ##写,但是会覆盖原来文件的内容,如果文件不存在,创建文件
f.write('wanghaha')
print(f.write)
f = open('/home/kiosk/file','a') ##追加,在原文件最后面写入内容
f.write('wawawa') ##操作
print(f.write)
f.close() ##关闭
f = open('/home/kiosk/file','r+') ##可读写,
print(f)
# c= f.read
# print(c)
f.write('1357')
print(f.write)
r+:
可读写,当文件不存在时会报错,默认情况下从指针所在位置开始写,并且会覆盖指针之后写入的位数
w+:
可读写,当文件不存在时,不会报错,但是会清空文件内容
a+:
可读写,当文件不存在时,不会报错,不会清空文件内容,在末尾追加
如果读取图片 音频 视频等非纯文本文件时候,需要通过二进制的方法进行读取
读取纯文本文件
r r+ w w+ a a+ ==rt rt+ wt wt+ at at+
读取二进制文件
rb rb+ wb wb+ ab ab+
f1 = open('1111.jpg',mode ='rb') #读取二进制文件内容
content = f1.read()
f1.close()
f2 = open('hello.jpg',mode='wb')
f2.write(content) ##写入要复制的文件,即读到的内容
f2.close()
默认情况下读取文件所有内容,小文件直接可以用read读取,
如果是一个大文件(文件大小>内存大小),用readline()
print(f.readline())
print(f.readline())
print(f.readlines())
seek指针:指针移动(seek(0,0))
第一个参数:偏移量,大于0,向后移动;小于0,向前移动;等于0,不移动
第二个参数:只有0,1,2三个数值选项
0表示移动指针到开头
1表示在当前位置
2表示指针到文件末尾
f = open('/home/kiosk/file' ,'r+')
print(f.readable()) ##判断是否可读,可读返回True
print(f.writable()) ##判断是否可写,可写返回Ture
print(f.readline()) ##逐行读取
print(f.tell()) ##显示指针位置
f.seek(10,0) ##移动指针
f.write('h1010') ##从指针所在的位置插入,写的就是字符串的内容
import random
f = open('/home/kiosk/file1', 'a+')
for i in range(10000):
f.write(str(random.randint(1,100))+ '\n') ##只有字符串才可以拼接,因此先转成字符串
f.seek(0,0) ##移动指针到开头,因为读的时候根据指针位置读取
print(f.read())
f.close()
f = open ('/home/kiosk/file1') #去掉|n
# print([line.strip() for line in f.readlines()])
# # print(list(map(lambda x:x.strip(),f.readlines())))
##linux下默认只能打开1024个文件
########with######
上下文管理器:打开文件,执行完with语句内容之后,自动关闭文件对象
f =open('/home/kiosk/file1')
with open('/home/kiosk/file1') as f:
print('with语句里面:',f.close())
print(f.close())
print('after with语句:' ,f.closed)
同时打开两个文件
with open('/home/kiosk/file1') as f1,open('/home/kiosk/file2','w+')as f2:
f2.write(f1.read()) ##将第一个文件的内容写入第二个文件中
f2.seek(0,0)
print(f2.read)
#python2中不支持同时打开两个文件
在python2.x中:
with open('/home/kiosk/file1')":
content=f1.read()
with open('/home/kiosk/file2') as f2:
f2.write(content)
随即生成一个mac地址
import string
import random
def create_mac(): ##生成一个mac地址
MAC = '01-AF-3B-'
num = string.hexdigits ##生成16进制数
for i in range(3):
n = random.sample(num,2) ##从16进制字符串中随意选出两个数字来
sn ='-'+''.join(n).upper() ##将小写转换成大写
MAC += sn
return MAC
def main(): ##主函数 ##生成100个mac地址
with open('mac.txt','w') as f:
for i in range(100):
mac = create_mac()
print(mac)
f.write(mac + '\n') ##每生成一个MAC地址,存入文件
main()
例子:京东二面笔试题:
# 1.生成一个大文件ips.txt,要求1200行,
# 每行随机为172.25.254.0/24段的ip;
# 2.读取ips.txt文件统计文件中的ip出现频率排前10的ip
import random
def create_ip_file(filename): ##生成ip函数
ips = ['172.25.254.'+str(i) for i in range(0,255)]
with open(filename,'a+') as f:
for count in range(1200):
f.write(random.sample(ips,1)[0] + '\n') ##取其中一个[0]表示取第一个
create_ip_file('ips.txt') ##保存到文件中
def sorted_ip(filename,count=10): ##排序函数
ips_dict=dict()
with open(filename) as f: ##统计文件中每个ip的个数,生成一个字典
for ip in f:
if ip in ips_dict:
ips_dict[ip] += 1
else:
ips_dict[ip] = 1
sorted_ip= sorted(ips_dict.items(),
key=lambda x:x[1],reverse=True)[:count] ##提取key-value值,根据value值从大到小排序,并截取前10
return sorted_ip
print(sorted_ip('ips.txt') )