实习—Python 基础

参考python核心编程第2版

第九章 文件的输入和输出

内置函数:open() 的基本语法:

file_object = open(file_name, access_mode='r', buffering=-1)
  • flie_name:文件路径名(相对或绝对路径)
  • access_mode:打开模式,具体见下表
  • buffering:设置访问文件所采用的缓冲方式,0表示不缓冲
文件模式操作
r以只读方式打开
rU 或 Ua以读方式打开, 同时提供通用换行符支持 (PEP 278)
w以写方式打开 (必要时清空)
a以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+以读写模式打开
w+以读写模式打开 (参见 w )
a+以读写模式打开 (参见 a )
rb以二进制读模式打开
wb以二进制写模式打开 (参见 w )
ab以二进制追加模式打开 (参见 a )
rb+以二进制读写模式打开 (参见 r+ )
wb+以二进制读写模式打开 (参见 w+ )
ab+以二进制读写模式打开 (参见 a+ )
x如果文件存在报错,不存在则创建
# 下面是一些打开文件的例子:
f = open('/etc/a') # 以读方式打开
f = open('a', 'w') # 以写方式打开
f = open('a', 'r+') # 以读写方式打开
f = open('a.sys', 'rb') # 以二进制读模式打开

bytes 字符串转换字节类型
n = bytes(李杰,encoding='utf-8')[转换的字符,可以是变量,转换后的编码]
字节转换为字符串
str(bytes(李杰,encoding='utf-8'),encoding='utf-8)
文件操作

文件读取

一些实例:

import os

if __name__ == '__main__':
    # 全文读取
    with open('file_tt/a.txt', encoding='utf-8') as f:
       contents = f.read()
       print(contents.rstrip())
    # 按行读取——1
    with open('file_tt/a.txt', encoding='utf-8') as f:
        for li in f:
            print(li)
    # 按行读取——2
    with open('file_tt/a.txt', encoding='utf-8') as f:
        lines = f.readlines()
    for line in lines:
        print(line)

    # 去除多余空行
    with open('file_tt/a.txt', encoding='utf-8') as f:
        lines = f.readlines()
    for line in lines:
        print(line.rstrip())    #str.rstrip([chars])
                                # chars -- 指定删除的字符(默认为空白符)

读取二进制文件:access_mode一般为rb。

# 常见方法
with open("sample.bin","rb") as f:
    data = f.read()
    
print(data[2])

# 还可以
import struct

with open("sample.bin","rb") as f:
    data = f.read()

unpack_result = struct.unpack('hhl', data[0:8])
print(unpack_result)

其中struct.unpack(fmt,string),解包。
fmt对应参数:hhl = short+short+long = 2 + 2 + 4 = 8 = [0:8]
在这里插入图片描述

文件写入

write() 方法。
mode=“w”,写模式,会重写文件;mode=“a”,追加模式,会在文件末尾添加数据。

if __name__ == '__main__':
    with open('file_tt/a.txt', 'w+', encoding='utf-8', mode='a') as f:
        f.write("aaab")

多线程编程

两大模块:thread模块、threading模块
老生常谈:进程和线程:一个线程只能属于一个进程,而一个进程可以有多个线程,但至少有一个线程

threading 模块

import threading

创建 Thread 对象有 2 种方法:

  • 直接创建 Thread ,将一个 callable 对象从类的构造器传递进去,这个 callable 就是回调函数,用来处理任务
  • 编写一个自定义类继承 Thread,然后复写 run() 方法,在 run() 方法中编写任务处理代码,然后创建这个 Thread 的子类

直接创建

from time import sleep, ctime
import threading

def loop0():
    print('start loop 0 at: ', ctime())
    sleep(4)
    print('loop 0 done at: ', ctime())

def loop1():
    print('start loop 1 at: ', ctime())
    sleep(2)
    print('loop 1 done at: ', ctime())

def main():
    print('starting at: ', ctime())
    #loop0()
    threading.Thread(target=loop0).start()
    threading.Thread(target=loop1).start()
    #loop1()
    print('all done at: ', ctime())

if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值