- 三目运算(三元运算)
a = 3
b =2
val = a if a>b else b - 文件读取
f = open(file = 'd:/MyPythonStudy/test.txt', mode = 'r', encoding= 'utf8')
data =f.read()
f.close
- encodeing= “编码方式” :: 保证和原文件编码方式
- 不知道文件编码方式时:
import chardet data = chardet.detect(open('test.txt', mode='rb').read()) print(data)
可得到编码方式.
- file= “路径” :: 可写相对路径,也可写绝对路径
- mode= “读取方式” ::: r 文本模式 b 二进制模式 w 写入模式 a 追加模式 wb w模式会删除原文件内容. r+ 读写模式 w+ 写读模式
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b5a0c8632f8fb528a6e682940b03ef12.png)
read() ## 按字符数读取
seek() tell() ## 按字节读取
truncate() ## 从当前光标位置截取/ truncate(6) ## 从头开始截6个字节
os.rename(file_new_name,file_name) ### 文件重命名 需要 import os