11-23

for循环访问文件

  • open打开文件的方式
序号方法及描述
r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
w打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
w+打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
  • 虚拟机上打开文件
以写的方式打开文件

In [1]: fd=open('/tmp/1.txt','w')

In [2]: fd
Out[2]: <_io.TextIOWrapper name='/tmp/1.txt' mode='w' encoding='UTF-8'>


In [3]: fd.buffer
            fd.buffer         fd.errors         fd.mode           fd.readline        
            fd.close          fd.fileno         fd.name           fd.readlines       
            fd.closed         fd.flush          fd.newlines       fd.seek           >
            fd.detach         fd.isatty         fd.read           fd.seekable        
            fd.encoding       fd.line_buffering fd.readable       fd.tell 
写数据进入文件
In [3]: fd.write('123\n')
Out[3]: 4

In [4]: quit
[root@localhost tmp]# cat 1.txt 
123

默认以读的方式打开文件
In [1]: fd=open('/tmp/1.txt')

读出文件内容
In [2]: fd.read()
Out[2]: '123\n'

追加的方式写入文件

In [8]: fd=open('/tmp/1.txt','a')

In [9]: fd.write('789\n')
Out[9]: 4

In [10]: fd=open('/tmp/1.txt')

In [11]: fd.read()
Out[11]: '456\n789\n'

一行一行的读取数据

In [27]: fd.readlines()
Out[27]: ['456\n', '789\n']

  • 编写一个读文件的python脚本
#! /bin/python

fd =open('/tmp/1.txt')
for line in fd.readlines():
    print(line,end="")
    
[root@localhost studypy]# python3 test_1123_1.py 
456
789

优化的写法
#! /bin/python

fd =open('/tmp/1.txt')
for line in fd:
    print(line,end="")

while遍历文件

#! /bin/python

fd = open('/tmp/1.txt')
while True:
    line = fd.readline()
    if not line:
        break
    print(line,end="")

[root@localhost studypy]# python3 test_1123_2.py 
456
789
  • with open的使用
#! /bin/python

with open('/tmp/1.txt') as fd:
    while True:
        line = fd.readline()
        if not line:
            break
        print(line,end="")

统计服务器的内存

#! /bin/python

with open('/proc/meminfo') as fd:
    for line in fd:
        if line.startswith('MemTotal'):
            total=line.split()[1]
        if  line.startswith('MemFree'):
            free=line.split()[1]

print("total is %d M,free is %d M."%(int(total)/1024,int(free)/1024))

数值类型转换

  • 十六进制和十进制
In [10]: int('0xa',16)
Out[10]: 10

In [11]: hex(10)
Out[11]: '0xa'


  • mac地址的转换
#!/bin/python

macaddr='00:0c:29:7e:fb:e7'
prefix=macaddr[:-3]
last_two=macaddr[-2:]
plus_one=int(last_two,16)+1
print(plus_one)
new_last_two=hex(plus_one)[2:]
print(new_last_two)
print("%s:%s"%(prefix,new_last_two))

[root@localhost studypy]# python3 test_1123_4.py
232
e8
00:0c:29:7e:fb:e8

数据类型装换(列表与字典相互转换)

  • 列表和字符转换
In [1]: s='abc'

In [2]: list(s)
Out[2]: ['a', 'b', 'c']

In [3]: l=list(s)

In [4]: l
Out[4]: ['a', 'b', 'c']

In [6]: ''.join(l)
Out[6]: 'abc'

  • 元组和字符转换和列表转换一样
In [7]: s='abc'

In [8]: tuple(s)
Out[8]: ('a', 'b', 'c')

In [9]: ''.join(tuple(s))
Out[9]: 'abc'

  • 字典和列表转换
In [14]: dic={'a':1,'b':2}

In [15]: dic.items()
Out[15]: dict_items([('a', 1), ('b', 2)])

In [16]: l1=dic.items()

In [17]: dict(l1)
Out[17]: {'a': 1, 'b': 2}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值