09

### 一 文件的扩展模式

(utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节)

#read()		功能: 读取字符的个数(里面的参数代表字符个数)
#seek()		功能: 调整指针的位置(里面的参数代表字节个数)
#tell()		功能: 当前光标左侧所有的字节数(返回字节数)

“”"
seek(0) 把光标移动到文件的开头
seek(0,2) 把光标移动到文件的末尾
“”"

1.r+ 先读后写

“”"
fp = open(“ceshi1.txt”,mode=“r+”,encoding=“utf-8”)
res = fp.read()
fp.write(“123”)
fp.seek(1)
res = fp.read()
print(res)
fp.close()
“”"

2.r+ 先写后读

“”"
fp = open(“ceshi1.txt”,mode=“r+”,encoding=“utf-8”)
fp.seek(0,2)
fp.write(“456”)
fp.seek(0)
res = fp.read()
print(res)
“”"

3.w+ 可读可写 (每次打开,都要清空重写)

fp = open(“ceshi2.txt”,mode=“w+”,encoding=“utf-8”)
fp.write(“你好世界”)
fp.seek(0)
res = fp.read()
print(res)
fp.close()

4.a+ 可读可写 (在写入内容时,会强制把光标移动到最后)

a模式和w模式,默认都可以创建文件

fp = open(“ceshi3.txt”,mode=“a+”,encoding=“utf-8”)
fp.write(“即实惠还管饱”)
fp.seek(0)
res = fp.read()
print(res)
fp.close()

fp = open(“ceshi1.txt”,mode=“r+”,encoding=“utf-8”)
res = fp.read(4)
res = fp.read(2)
fp.seek(2)
total = fp.tell()
print(total)
fp.close() #功能: 当前光标左侧所有的字节数(返回字节数)

### 四.with 语法 (可以省略掉close操作)

“”"
with open(…) as fp:
code…
code…
“”"

复制图片?(二进制的字节流)

with open(“集合.png”,mode=“rb”) as fp:
str_bytes = fp.read()

with open(“集合2.jpg”,mode=“wb”) as fp:
fp.write(str_bytes)

在升级,三行代码实现

with open(“集合.png”,mode=“rb”) as fp1 , open(“集合3.jpg”,mode=“wb”) as fp2:
str_bytes = fp1.read()
fp2.write(str_bytes)

### 1.close 的意义

刷新缓冲区 flush

# 当文件关闭的时候自动刷新缓冲区
# 当整个程序运行结束的时候自动刷新缓冲区
# 当缓冲区写满了  会自动刷新缓冲区
# 手动刷新缓冲区

“”"
fp = open(“ceshi6.txt”,mode=“a+”,encoding=“utf-8”)
fp.write(“add”)

fp.flush()

while True:

# pass

fp.close()
“”"

### 2.文件相关的函数

#readable() 功能: 判断文件对象是否可读
#writable() 功能: 判断文件对象是否可写

fp = open(“ceshi6.txt”,mode=“w+”,encoding=“utf-8”)
res1 = fp.readable()
res2 = fp.writable()
print(res1,res2)

#readline() 功能: 读取一行文件内容
‘’’
with open(“ceshi6.txt” , mode=“r+” ,encoding=“utf-8”) as fp:
res = fp.readline(20000)
“”"
如果字符个数 > 当前行总个数 ,按照当前行进行读取
如果字符个数 < 当前行总个数 ,按照实际个数读取
“”"

# res = fp.readline()
# res = fp.readline()
# res = fp.readline()
# res = fp.readline()
print(res)

改造 => 通过循环把文件当中所有内容读取出来

with open(“ceshi6.txt” , mode=“r+” ,encoding=“utf-8”) as fp:
# 先读取一行
res = fp.readline()
# 判断读取的字符串是不是空,不为空就执行,反之不执行;
while res:
print(res)
res = fp.readline()
‘’’
print("<===>")
#readlines() 功能:将文件中的内容按照换行读取到列表当中

lst_new = []
with open(“ceshi1.txt” , mode=“r+” ,encoding=“utf-8”) as fp:
lst = fp.readlines()
for i in lst:
res = i.strip()
lst_new.append(res)
print(lst_new)

#writelines() 功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据

可迭代数据(容器类型数据 , range对象, 迭代器) 文件对象

文件对象也是可迭代性数据,在遍历时候,默认按照一行一行进行读取

lst = [“抬头思故乡\n”,“疑是地上霜\n”,“举头望明月\n”,“低头鞋两双\n”]
with open(“ceshi7.txt”,mode=“w+” ,encoding=“utf-8”) as fp:

在列表中,对应的位置,插入想要的字符串

lst.insert(1,"黄俊真聪明\n")
fp.writelines(lst)

#truncate() 功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)

截取 -> 清空 -> 写入

with open(“ceshi7.txt”,mode=“r+”,encoding=“utf-8”) as fp:
fp.truncate(6)

总结:

“”"
read(字符)
readline(字符)
seek(字节)
truncate(字节)
“”"

### 函数 : 功能(包裹一部分代码,实现某一个功能,达成某一个目的)

“”"
特点:可以反复调用,提高代码的复用性,提升开发效率,便于后期维护
“”"

函数的基本格式

“”"

1.定义一个函数

def 函数名():
code1
code2

2.调用函数

函数名()
“”"

函数的定义处

def func():
print(“我是一个函数”)

函数的调用处

func()

函数的名字

“”"
函数的命名
字母数字下划线,首字符不能为数字
严格区分大小写,且不能使用关键字
函数命名有意义,且不能使用中文哦

驼峰命名法:
(1) 大驼峰命名法: 每个单词的首字符都大写 mycar => MyCar (面向对象当中,定义类class)
(2) 小驼峰命名法: 除了第一个单词小写之外,剩下每个单词首字符大写 mycar => myCar (函数,变量)
命名一个函数 通常用_拼接的形式,组装不同的单词
mycar => my_car
symmetric_difference
“”"

函数的定义处

def cfb_99():
for i in range(1,10):
for j in range(1,i+1):
print("%d*%d=%2d " % (i,j,i*j) , end="")
print()

函数的调用

for i in range(10):
cfb_99()

### 函数的参数 : (参数是配合函数运行时,需要用到的值)

“”"
参数的种类:
(1) 形参: 形式上的参数 , 在函数的定义处
(2) 实参: 实际上的参数 , 在函数的调用处

形参的种类:
普通(位置)形参,默认形参,普通收集参数,命名关键字参数,关键字收集参数
实参的种类:
普通实参,关键字实参

遵循的原则:
调用参数的时,形参和实参必须一一对应,否则报错
“”"

(1) 普通形参

函数的定义处

“”“hang , lie 是普通形参(位置形参)”""
def star(hang,lie):
i = 0
while i<hang:
j = 0
while j<lie:
print("*",end="")
j+=1
print()
i+=1

函数的调用处

“”“4,9 是实参,是普通实参”""
star(4,9)

(2) 默认形参

“”“hang和lie身上带有默认值”""
def star(hang=10,lie=10):
i = 0
while i<hang:
j = 0
while j<lie:
print("*",end="")
j+=1
print()
i+=1
“”"
调用时,如果不给实际参数,默认使用自带的值进行调用
调用时,如果给实际参数,那么使用实参来进行调用
“”"
star()

print("<====>")

(3)普通形参 + 默认形参

“”“默认参数必须跟在普通参数的后面”""
def star(hang,lie=10):
i = 0
while i<hang:
j = 0
while j<lie:
print("*",end="")
j+=1
print()
i+=1

star(20)

(4) 关键字实参

def star(hang,lie=10):
i = 0
while i<hang:
j = 0
while j<lie:
print("*",end="")
j+=1
print()
i+=1

关键字实参

“”“调用函数时,关键字实参顺序可以任意调整的”""
star(hang=3,lie=8)
star(lie=8,hang=3)

(5) 普通实参 + 关键字实参

“”“关键字实参必须跟在普通实参的身后”""
def star(hang,a,b,c,lie=10):
i = 0
while i<hang:
j = 0
while j<lie:
print("*",end="")
j+=1
print()
i+=1

调用处

star(hang = 2,a=3,b=4,c=5,lie=6)

star(lie=6,a=3,b=4,c=5,hang = 2)

star(3,3,4,5,lie=4)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值