栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

实习—Python 基础

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实习—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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843991.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号