python之文件

纯文本文件

文件读取的三部曲:打开 —> 操作 ----> 关闭

r(默认参数):

-只能读,不能写
-读取文件不存在 会报错
指针从零开始

w(写)

-write only
-文件不存在,会自动创建新的文件
-文件存在,会清空文件内容并写入新的内容
指针从零开始

a(追加):

-write only
-写:文件不存在,不会报错,会创建新的文件并写入内容
-写:文件存在,不会清空文件的内容,会在文件末尾追加
指针不会从零开始

r+

-可读可写
-文件不存在,报错
-默认情况下,从【文件指针】所在位置开始写入

w+

-可读可写
-文件不存在,不报错
-会清空文件内容

a+

-可读可写
-文件不存在,不报错
-不会清空文件,在末尾追加

seek:指针移动

第一个参数:偏移量 >0:代表向后移动 <0 代表向前移动
第二个参数:
0:移动指针到文件开头
1:当前位置
2:移动指针到末尾

1.打开文件

f = open('/tmp/westos','r')

2.操作文件

print(f)
读文件
# content = f.read()      使得文件指针从头走到尾
# print(content)
写文件

告诉当前的文件指针所在的位置

# print(f.tell())
# f.write('westos')
# print(f.tell())
# f.write('redhat')
# print(f.tell())
判断文件对象拥有的权限
print(f.readable())
print(f.writable())
print(f.tell())
content = f.read()
print(content)
print(f.tell())
content1 = f.read()
print(content1)
# print(content)

3.关闭文件

f.close()

‘r’

【默认是只读模式】

f = open('/tmp/passwd')
print(f.tell())
cont=f.read()
print(cont)
print(f.tell())
co=f.read()
print(co)
print(f.tell())
f.close()
==================
0
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin

143

143

‘r+’

【cosmos六个字节,ancestor八个字节】

f=open('/tmp/passwd','r+')
print(f.tell())
con=f.read()
print(f.tell())
f.write('cosmos')
print(con)
print(f.tell())
f.write('ancestor')
print(con)
print(f.tell())
====================
0
143
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin

149
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin

157

这里我们发现:写入的cosmos和ancestor并没有被读出来,这是因为此时的指针已经到了最后.能读出是因为指针一开始在开端

[kiosk@jing bin]$ cat /tmp/passwd 
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
cosmosancestor[kiosk@jing bin]$

‘w’

f=open('/tmp/passwd123','w')
con=f.read()
print(con)

io.UnsupportedOperation: not readable


f=open('/tmp/passwd123','w')
print(f.tell())
f.write('666')
print(f.tell())

0
3

‘w+’
python3中.seek()只能从开头索引即(0,0),要用当前’1’和结尾’2’需要加mode=‘b’
移动了指针的位置再重新读取
[此时passwd里面的内容是content]]

f1=open('/tmp/passwd','rb+')
content=f1.read()
f1.close()
f = open('/tmp/passwd123', 'wb+')
con = f.read()
print(f.tell())
print(con)
f.write(content)
print(f.tell())
f.seek(-5,1)
con1=f.read()
print(con1)
print(f.tell())
==================
0
b''
7
b'ntent'
7

‘a’

‘a+’

f=open('/tmp/passwd123','w')
f.write('''
zhou
jing
cosmos
fairy
lalala
beauty
zizizi
''')
f.close()
f=open('/tmp/passwd123','r')
print(f.readline())
print(f.tell())
print(f.readline())
print(f.tell())
print(f.readline())
print(f.tell())
==========================


1
zhou

6
jing

11
指针在哪里写入的位置就在哪里

w 和 r 都是从头开始写入[会覆盖原文件内容]
a 是追加 指针在尾部

 f = open('/tmp/passwd', 'w+')
 print(f.tell())
f.write('first')
print(f.tell())
f.seek(0.0)
con = f.read()
print(f.tell())
f.write('fairy')
print(con)
print(f.tell())

0
5
5
first
10

f = open('/tmp/passwd', 'r+')
print(f.tell())
f.write('lie123')
print(f.tell())
f.seek(0.0)
con = f.read()
print(f.tell())
f.write('fairy')
print(con)
print(f.tell())

0
6
10
lie123airy
15

f = open('/tmp/passwd', 'a+')
print(f.tell())
f.write('cosmos')
print(f.tell())
f.seek(0.0)
con = f.read()
print(f.tell())
f.write('jing')
print(con)
print(f.tell())

15
21
21
lie123airyfairycosmos
25

非纯文本文件

如果读取是 图片 音频 视频 ,需要通过二进制的方式读取和写入

r r+ w w+ a a+ == rt rt+ wt wt+ at at+
注意:其中hello.jpg是相对位置哟,当然也可以是绝对的位置.自行练习

# 先读取二进制文件内容
f1 = open('hello.jpg',mode='rb')     ##mode= 可以去掉,直接写'rb'
content = f1.read()
f1.close()

f2 = open('lucky.jpg',mode='wb')     ##同上
# 写入要复制的文件的内容
f2.write(content)
f2.close()

readline()&readlines()

“”"
默认情况下读取文件的内容 小的文件:直接用read读取即可
如果是一个大文件(文件大小>=内存大小)
用readline()
“”"
按行读取
print(f.readline())
print(f.readline())

按字节读取
print(f.read(3))

读取文件内容,并返回一个列表,列表元素分别为文件每行的内容
f1=open('/tmp/passwd')
print(f1.readlines())
['hello\n', 'hi\n', 'are u ok\n', 'yeah \n', 'good\n', 'ready\n', 'go\n']

---------------------练习-----------------------

读取内容,返回一个列表,去掉后面的\n
f = open('/tmp/passwd')
#   print(list(map(lambdax:x.strip(),f.readlines())))
print([line.strip() for line in f.readlines()])
f.close()

----------------------练习-----------------------

创建文件data.txt 文件共100000行, 每行存放以一个1~100之间的整数
import random

f = open('data.txt','a+')      ##'w'也可以
for i in range(100000):
    f.write(str(random.randint(1,100)) + '\n')
f.close()

上下文管理器

-----------打开文件,执行完with语句内容之后,自动关闭文件【不需要file.close( )】

with open('/tmp/passwd') as f:
    print(f.read())



# 将第一个文件的内容写道第二个文件中
with open('data.txt') as f1,open('data2.txt','w+') as f2:
    f2.write(f1.read())
    f2.seek(0,0)
    print(f2.read())

"""
python2.x
with open(data.txt) as f1:
    content = f1.read()
with open(data2.txt,'w+') f2:
    f2.write(content)
"""

--------------------练习-------------------------

生成100个MAC地址并写入文件中,MAC地址前6位(16进制)为01-AF-3B
格式为:01-AF-3B-xx-xx-xx

import string
import random 
def creat_mac():
    hex = string.hexdigits
    str = '01-AF-3B'
    for i in range(3):
        str1 = random.sample(hex, 2)
        str += '-' + ''.join(str1)
    return str

def born():
    with open('mac.txt', 'w') as f:
        for i in range(10):
            f.write(creat_mac() + '\n')

born()

在这里插入图片描述

------------------练习-------------------

京东测试题

生成一个大文件ips.txt,要求1200行,每行随机为172.25.254.0/24段的ip;
读取ips.txt文件统计这个文件中ip出现频率排行前10的ip;

import random

def create_ip():
    exip='172.25.254.'
    s=exip+str(random.randint(0,255))
    return s

def born_ip():
    with open('ips.txt','w') as f:
        for i in range(1200):
            ip=create_ip()
            f.write(ip+'\n')
born_ip()


with open('ips.txt','r') as f:
    ip_sum={}
    for i in f:
        if i in ip_sum:
            ip_sum[i]+=1
        else:
            ip_sum[i]=1
    ip_play=sorted(ip_sum.items(),key=lambda x:x[1],reverse=True)
    print(ip_play)
 ==========================OR==========================
 import random

def create_ip_file(filename):
    ips = ['172.25.254.' + str(i) for i in range(0,255)]
    print(ips)
    with open(filename,'a+') as f:
        for count in range(1200):
            f.write(random.sample(ips,1)[0] + '\n')

#create_ip_file('ips.txt')

def sorted_ip(filename,count=10):
    ips_dict = dict()
    with open(filename) as f:
        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]
    return sorted_ip
print(sorted_ip('ips.txt',20))

[('172.25.254.161\n', 11), ('172.25.254.200\n', 11), ('172.25.254.70\n', 10), ('172.25.254.225\n', 10), ('172.25.254.12\n', 10), ('172.25.254.240\n', 9), ('172.25.254.32\n', 9), ('172.25.254.206\n', 9), ('172.25.254.131\n', 9), ('172.25.254.150\n', 9), ('172.25.254.124\n', 9), ('172.25.254.251\n', 8), ('172.25.254.116\n', 8), ('172.25.254.42\n', 8), ('172.25.254.153\n', 8), ('172.25.254.63\n', 8), ('172.25.254.255\n', 8), ('172.25.254.94\n', 8), ('172.25.254.91\n', 8), ('172.25.254.221\n', 8), ('172.25.254.125\n', 8), ('172.25.254.43\n', 8), ('172.25.254.165\n', 8), ('172.25.254.151\n', 8), ('172.25.254.19\n', 8), ('172.25.254.215\n', 7), ('172.25.254.1\n', 7), ('172.25.254.135\n', 7), ('172.25.254.243\n', 7), ('172.25.254.142\n', 7), ('172.25.254.51\n', 7), ('172.25.254.184\n', 7), ('172.25.254.30\n', 7), ('172.25.254.250\n', 7), ('172.25.254.228\n', 7), ('172.25.254.163\n', 7), ('172.25.254.66\n', 7), ('172.25.254.73\n', 7), ('172.25.254.84\n', 7), ('172.25.254.192\n', 7), ('172.25.254.127\n', 7), ('172.25.254.166\n', 7), ('172.25.254.254\n', 7), ('172.25.254.145\n', 7), ('172.25.254.241\n', 7), ('172.25.254.133\n', 7), ('172.25.254.146\n', 7), ('172.25.254.209\n', 7), ('172.25.254.129\n', 7), ('172.25.254.213\n', 7), ('172.25.254.13\n', 7), ('172.25.254.10\n', 7), ('172.25.254.11\n', 7), ('172.25.254.172\n', 7), ('172.25.254.189\n', 6), ('172.25.254.58\n', 6), ('172.25.254.77\n', 6), ('172.25.254.41\n', 6), ('172.25.254.188\n', 6), ('172.25.254.106\n', 6), ('172.25.254.242\n', 6), ('172.25.254.248\n', 6), ('172.25.254.152\n', 6), ('172.25.254.14\n', 6), ('172.25.254.76\n', 6), ('172.25.254.105\n', 6), ('172.25.254.64\n', 6), ('172.25.254.109\n', 6), ('172.25.254.101\n', 6), ('172.25.254.253\n', 6), ('172.25.254.216\n', 6), ('172.25.254.46\n', 6), ('172.25.254.110\n', 6), ('172.25.254.234\n', 6), ('172.25.254.117\n', 6), ('172.25.254.138\n', 6), ('172.25.254.201\n', 6), ('172.25.254.5\n', 6), ('172.25.254.156\n', 6), ('172.25.254.92\n', 6), ('172.25.254.65\n', 6), ('172.25.254.210\n', 6), ('172.25.254.207\n', 6), ('172.25.254.121\n', 5), ('172.25.254.93\n', 5), ('172.25.254.7\n', 5), ('172.25.254.71\n', 5), ('172.25.254.79\n', 5), ('172.25.254.179\n', 5), ('172.25.254.183\n', 5), ('172.25.254.238\n', 5), ('172.25.254.203\n', 5), ('172.25.254.136\n', 5), ('172.25.254.155\n', 5), ('172.25.254.185\n', 5), ('172.25.254.53\n', 5), ('172.25.254.249\n', 5), ('172.25.254.34\n', 5), ('172.25.254.9\n', 5), ('172.25.254.2\n', 5), ('172.25.254.56\n', 5), ('172.25.254.35\n', 5), ('172.25.254.29\n', 5), ('172.25.254.160\n', 5), ('172.25.254.47\n', 5), ('172.25.254.164\n', 5), ('172.25.254.24\n', 5), ('172.25.254.147\n', 5), ('172.25.254.231\n', 5), ('172.25.254.187\n', 5), ('172.25.254.229\n', 5), ('172.25.254.81\n', 5), ('172.25.254.223\n', 5), ('172.25.254.237\n', 5), ('172.25.254.44\n', 5), ('172.25.254.97\n', 5), ('172.25.254.244\n', 5), ('172.25.254.0\n', 5), ('172.25.254.218\n', 5), ('172.25.254.186\n', 5), ('172.25.254.72\n', 5), ('172.25.254.113\n', 5), ('172.25.254.143\n', 5), ('172.25.254.128\n', 5), ('172.25.254.173\n', 5), ('172.25.254.180\n', 5), ('172.25.254.112\n', 5), ('172.25.254.99\n', 5), ('172.25.254.3\n', 5), ('172.25.254.69\n', 5), ('172.25.254.54\n', 5), ('172.25.254.100\n', 5), ('172.25.254.20\n', 5), ('172.25.254.88\n', 5), ('172.25.254.198\n', 5), ('172.25.254.89\n', 5), ('172.25.254.59\n', 5), ('172.25.254.167\n', 4), ('172.25.254.246\n', 4), ('172.25.254.220\n', 4), ('172.25.254.236\n', 4), ('172.25.254.49\n', 4), ('172.25.254.168\n', 4), ('172.25.254.193\n', 4), ('172.25.254.141\n', 4), ('172.25.254.162\n', 4), ('172.25.254.159\n', 4), ('172.25.254.33\n', 4), ('172.25.254.80\n', 4), ('172.25.254.67\n', 4), ('172.25.254.16\n', 4), ('172.25.254.78\n', 4), ('172.25.254.232\n', 4), ('172.25.254.8\n', 4), ('172.25.254.27\n', 4), ('172.25.254.208\n', 4), ('172.25.254.90\n', 4), ('172.25.254.103\n', 4), ('172.25.254.247\n', 4), ('172.25.254.204\n', 4), ('172.25.254.82\n', 4), ('172.25.254.191\n', 4), ('172.25.254.60\n', 4), ('172.25.254.6\n', 4), ('172.25.254.195\n', 4), ('172.25.254.227\n', 4), ('172.25.254.48\n', 4), ('172.25.254.224\n', 4), ('172.25.254.83\n', 4), ('172.25.254.98\n', 4), ('172.25.254.233\n', 4), ('172.25.254.170\n', 4), ('172.25.254.96\n', 4), ('172.25.254.137\n', 4), ('172.25.254.171\n', 4), ('172.25.254.217\n', 4), ('172.25.254.126\n', 4), ('172.25.254.18\n', 3), ('172.25.254.154\n', 3), ('172.25.254.132\n', 3), ('172.25.254.38\n', 3), ('172.25.254.122\n', 3), ('172.25.254.199\n', 3), ('172.25.254.75\n', 3), ('172.25.254.118\n', 3), ('172.25.254.182\n', 3), ('172.25.254.74\n', 3), ('172.25.254.50\n', 3), ('172.25.254.120\n', 3), ('172.25.254.4\n', 3), ('172.25.254.158\n', 3), ('172.25.254.21\n', 3), ('172.25.254.17\n', 3), ('172.25.254.86\n', 3), ('172.25.254.119\n', 3), ('172.25.254.245\n', 3), ('172.25.254.140\n', 3), ('172.25.254.157\n', 3), ('172.25.254.31\n', 3), ('172.25.254.85\n', 3), ('172.25.254.130\n', 3), ('172.25.254.219\n', 3), ('172.25.254.23\n', 3), ('172.25.254.25\n', 3), ('172.25.254.87\n', 3), ('172.25.254.194\n', 3), ('172.25.254.212\n', 3), ('172.25.254.211\n', 3), ('172.25.254.222\n', 3), ('172.25.254.22\n', 3), ('172.25.254.114\n', 3), ('172.25.254.102\n', 3), ('172.25.254.15\n', 3), ('172.25.254.40\n', 2), ('172.25.254.169\n', 2), ('172.25.254.57\n', 2), ('172.25.254.111\n', 2), ('172.25.254.205\n', 2), ('172.25.254.148\n', 2), ('172.25.254.55\n', 2), ('172.25.254.174\n', 2), ('172.25.254.45\n', 2), ('172.25.254.134\n', 2), ('172.25.254.196\n', 2), ('172.25.254.197\n', 2), ('172.25.254.26\n', 2), ('172.25.254.202\n', 2), ('172.25.254.62\n', 2), ('172.25.254.28\n', 2), ('172.25.254.115\n', 2), ('172.25.254.139\n', 2), ('172.25.254.214\n', 2), ('172.25.254.61\n', 2), ('172.25.254.123\n', 2), ('172.25.254.226\n', 2), ('172.25.254.95\n', 2), ('172.25.254.108\n', 2), ('172.25.254.235\n', 2), ('172.25.254.39\n', 1), ('172.25.254.104\n', 1), ('172.25.254.52\n', 1), ('172.25.254.230\n', 1), ('172.25.254.178\n', 1), ('172.25.254.149\n', 1), ('172.25.254.107\n', 1), ('172.25.254.68\n', 1), ('172.25.254.176\n', 1), ('172.25.254.239\n', 1), ('172.25.254.177\n', 1), ('172.25.254.190\n', 1), ('172.25.254.36\n', 1), ('172.25.254.37\n', 1), ('172.25.254.252\n', 1), ('172.25.254.175\n', 1), ('172.25.254.181\n', 1)]

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值