Python学习(是我的课堂上学到的东西)不包含课外

2023-10-24课堂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"""
文件读写
使用的是语法糖,无需考虑释放问题
"""
with open("test.txt","r",encoding="UTF-8") as file:
# 全部读取
data = file.read()
print(data)
with open("test.txt","r",encoding="UTF-8") as file:
# 读一行内容
line = file.readline()
print(line)
with open("test.txt","r",encoding="UTF-8") as file:
# 读全部行内容
lines = file.readlines()
print(lines)
1
2
3
4
5
6
7
8
9
10
11
"""
文件指针
"""
with open(file="./data.txt", mode="r") as file:
# 返回当前文件指针位置
print(file.tell())
print(file.read())
# 移动文件指针位置
file.seek(0)
# 返回文件指针位置
print(file.readline())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 导入模块
import os

# 获取指定文件的绝对路径
abspath = os.path.abspath("001-os模块.py")
print(abspath)
# 获取路径中的文件名部分
file_name = os.path.basename(abspath)
print(file_name)
# 获取路径中文件夹部分
file_dir = os.path.dirname(abspath)
print(file_dir)
# 判断路径是否存在
is_exist = os.path.exists(abspath)
print(is_exist)
# 判断是否是文件
print(os.path.isfile(abspath))
# 判断是否是目录
print(os.path.isdir(abspath))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
""""
os模块文件操作
"""

# 导入模块
import os

# 创建文件(没有文件就创建文件)
open(file="a.txt", mode="w", encoding="utf-8")

# 重命名文件
os.rename("a.txt", "b.txt")

# 设置 休眠
os.sleep(10)

# 删除文件
os.remove("b.txt")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
os模块目录操作
"""

# 导入模块
import os

# 创建单级目录
if not os.path.exists("./a"):
os.mkdir("./a")

# 重命名目录
os.rename("./a", "./b")

# 删除目录
os.rmdir("./b")

# 创建多级目录
os.makedirs("./a/b/c")
os.makedirs("./d/e/f/h.txt")
# 删除多级目录
os.removedirs("./a/b/c")
os.removedirs("./d/e/f/h.txt")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""
os模块文件读写
"""

# 导入模块
import os

# 写入文件
writer = os.open(path="./a.txt",flags=os.O_CREAT | os.O_WRONLY)

# 直接写入字符串会报错

content = b"this is a test"
os.write(writer, content)

# 读文件
reader = os.open(path="./a.txt", flags=os.O_RDONLY)
data = os.read(reader,os.path.getsize("./a.txt"))
print(data.decode("utf-8"))
1
2
3
4
5
6
7
8
9
10
11
"""
os模块执行命令
"""

# 导入模块
import os

# 执行命令(运行当前目录指定的python文件)
os.system("python a.py")
# 列出当前目录
os.system("dir")
1
2
3
4
5
6
7
8
import os
# 这个是删除文件里面的文档的操作方法,需要去掌握一下
if os.path.exists("./datas"):
files = os.listdir("./datas")
for file in files:
if os.path.isfile(os.path.abspath("./datas/"+file)):
os.remove(os.path.abspath("./datas/"+file))
print("删除成功")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
python异常处理
"""
# NameError
# 名字找不到
# print(name)
# ZeroDivisionError
# 除零
# print(12/0)
# IndexError
# 类型错误
# print("jack"[10000])
# FileNotFoundError
# 文字找不到
# file = open("jnb.json", mode="r")

num1 = int(input("被除数: "))
num2 = int(input("除数: "))
try:
rel = num1 / num2
print(rel)
except ZeroDivisionError as e:
print(e)
print("除数为零了,请注意!")
else:
print("继续执行程序!")
finally:
print("关闭资源")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
file = "./demo"
try:
string = "hello"
index = int(input("输入索引: "))
print(string[index])
filename = input("输入文件名: ")
file = open(filename,mode="r", encoding="UTF-8")
print(file.read())
except TypeError as e:
print(e)
print("类型错误")
except IndexError as e:
print(e)
print("索引越界")
except FileNotFoundError as e:
print(e)
print("指定的文件不存在")
finally:
if file:
file.close()
else:
print("errors")