'''
语法:open(file,mode = 'r', *, encoding=None) -- 以指定方式打开指定文件
a. file --- 字符串;需要打开的文件路径
文件路径可以写绝对路径也可以写相对路径
1. 绝对路径: 文件在计算机的全路径(windows是从盘名开始写,mac或者linux从系统根目录或者用户根目录开始写)
open(r'/c:/names/users/abc/files/a.txt') #r 是电脑一般斜杠是反的 所以r确保斜杠对
路径名需要引号框起来
2.相对路径 :如果使用相对路径必须先将需要打开的文件直接或者间接放在项目里
a. 用 .开头, .表示当前目录(当前代码文件所在文件夹)
open('./files/data.txt') 相对路径如果以 './'开头,'./'可以省略
b. 以 ..开头 ,用..表示当前目录上次目录
b. mode 字符串,文件的打开方式 -- 决定打开文件后能做什么(读?写?)
决定操作文件的时候对应的数据的数据类型
根据mode的功能他的值有两组:
1. 第一组值 - 决定打开文件后能干什么(读还是写)
r - 只读
w - 只写;在打开文件时会清空文件内容
a(append mode) - 只写;在打开文件的时候会保留文件内容
注意: 以读的方式打开一个不存在的文件程序会报错,
以写的方式打开不存在文件不会报错 且会自动创建这个文件
2. 第二组值 -- 决定操作文件的时候对应的数据的数据类型
t -text mode(默认值) - 读写数据必须是字符串
b - 读写数据必须是字节(bytes) (一般是视频、音频、图片,非文本文件只能用b方式打开)
注意: 给mode 赋值时候必须在两组值中每一组中选择一个,第二组值不选表示选t,两组值顺序不影响
c. encoding 编码方式: 1.gbk: 只支持英文、中文、韩文和日文编码,英文是单字节编码,用双字节存储一个中文字符
2.utf-f: 支持所有语言,用三个字节存储一个中文字符
3.(8bit = 1bytes 1kb =1024bytes 1mb = 1024kb 1G = 1024mb)
不同文本文件编码方式在保存同一个字符的编码值的时候采用字节数可能不同
使用文本文件编码方式的基本原则:将数据写入文件时使用的编码方式必须和读文件中内容时采用编码方式一致
'''
open('/Users/yaoxin/Documents/afile.txt' ) #绝对路径
open('./afile1.txt') # afile1 和 我这个代码文件同级,所以直接当前目录就可以, ./也可以省略
open('../day03/afile1.txt') #当前代码文件上一级是week03,week03/day03/afile1
f = open('afile1.txt','r')
f.read()
f.write('phth') #会报错,因为现在是r(只读模式)
f = open('afile1.txt','a')
f.read() #报错,现在是append mode 模式,
f.write('python') 只写,且会在原文件内容后添加
f = open('afile1.txt','w') # 打开时,原文件内容会被清空,如果文件不存在,会自动创建
f = open('afile1.txt','rt')
result = f.read()
print(type(result)) # str
f = open('afile1.txt','rb')
result = f.read()
print(type(result)) # bytes
with open('../day03/afile1.txt','r',encoding='utf-8') as f:
print(f.read())
f = open('afile1.txt', 'rt',encoding='utf-8')
result = f.read()
print(result)
## 这样再赋值无结果
result1 = f.read()
print(result1)
# 再open一次,可以再输出
f = open('afile1.txt', 'rt',encoding='utf-8')
result = f.read()
print(result)
#或者seek(0) 移动到开头
f.seek(0)
result1 = f.read()
print(result1)
f = open('afile1.txt', 'rt',encoding='utf-8')
#result = f.readline()
print(f.readline()) #hellohello世界hello世界hello世界hello世界hello世界hhellohello
print(f.readline()) #dfdfdfdf的大方大方
print(f.readline()) #print(f.readline())
f = open('afile1.txt', 'rt',encoding='utf-8')
while True:
result = f.readline()
print(result)
if not result:
break
f = open('afile1.txt', 'wt',encoding='utf-8')
f.write('你好世界')
f = open('afile1.txt', 'r',encoding='utf-8')
print(f.read())
f.close()
#count.txt 的内容就是0
#获取保存在文件中上一次程序的启动次数
f = open('count.txt','r',encoding='utf-8')
count = int(f.read()) #count 文件里就只有一个0,读取他出来
f.close()
count += 1
print(count)
# 将最新的次数更新到文件中
f = open('count.txt','w', encoding='utf-8')
f.write(str(count)) #count 是int 要转换成 str才可以写入
f.close()
# 练习1:写程序录入学生的名字,录入完后打印目前已经录入过的所有的学生的名字(每次运行程序只录入一个)
"""
请输入学生的名字:小名
小名
请输入学生的名字: 张三
小名 张三
请输入学生的名字: 李四
小名 张三 李四
...
"""
录入学生的名字,录入完后打印目前已经录入过的所有的学生名字
student_name = input('请输入学生的姓名:')
file1 = open('afile1.txt','at',encoding='utf-8')
file1.write(student_name)
# 练习2:写程序录入学生的名字,录入完后打印目前已经录入过的所有的学生的名字,但是每个名字只能录入一次!
student_name = input('请输入学生的姓名:')
# 读取文件内容
file1 = open('afile1.txt', 'rt', encoding='utf-8')
file1_content = file1.read()
file1.close()
if student_name in file1_content:
print('名字已经存在')
else:
file1 = open('afile1.txt', 'at', encoding='utf-8')
file1.write(student_name + ' ')
file1.close()
print('名字已添加到文件中')
# 重新读取文件内容并打印
file1 = open('afile1.txt', 'rt', encoding='utf-8')
updated_content = file1.read()
file1.close()
print(updated_content)
re模块 -- 是python自带的模块,这个模块中提供了各种各样和正则相关的函数
fullmatch函数: fullmatch(正则表达式, 字符串) --- 判断整个字符串是否符合正则表达式所描述的规则,如果不符合结果为None, 符合结果是匹配对象
python中正则表达式写法: r‘正则表达式内容’
result = fullmatch(r'abc','abc')
print(result)
# . 表示任意字符, 就是说第一个随便是什么,后面两个是 bc 就行,长度也要匹配
result = fullmatch(r'.bc','kbc')
print(result)
result = fullmatch(r'..bc..','abbcld')
print(result)
result = fullmatch(r'\dbc','9bc')
print(result)
#任意三个数字字符
result = fullmatch(r'\d\d\d','365')
print(result)
result = fullmatch(r'\d\s\d','2\n5')
print(result)
result = fullmatch(r'1\w3','1是3')
print(result)
result = fullmatch(r'1[abc]2','1c2')
print(result)