def main():
try:
# 这里加不加r其实都一样,默认的就是r xxxx自己替换成你的文件名字
file = open("E:\\xxxx", "r");
print("文件名称:{}".format(file.name));
print("文件是否已关闭:{}".format(file.closed));
print("文件访问模式:{}".format(file.mode));
except BaseException:
pass;
finally:
file.close();
print("调用close()关闭文件对象");
if __name__ == '__main__':
main();
def main():
try:
# w代表输出 文件不存在时,自动创建,创建成功之后写入数据
file = open("E:\\test.txt", "w");
file.write("奥特曼打小怪兽");
except BaseException:
pass;
finally:
file.close();
if __name__ == '__main__':
main();
Use with to perform file operations
def main():
with open("E:\\test.txt","w") as file:
file.write("黑猫警长抓小老鼠");
if __name__ == '__main__':
main();
The a of the test file operation represents appending data to the file
def main():
with open("E:\\test.txt","a") as file:
for item in range(10):
file.write("黑猫警长抓小老鼠\n");
if __name__ == '__main__':
main();
Use r to read the contents of the file
def main():
# 使用r来读取数据
with open("E:\\test.txt","r") as file:
# 读取一行
var = file.readline();
# 当一行数据存在时
while var:
print(var,end="");
# 继续读取下一行
var = file.readline();
if __name__ == '__main__':
main();
Use r to read file content optimized version
def main():
# 使用r来读取数据
with open("E:\\test.txt","r") as file:
# 迭代文件对象
for line in file:
# 输出每行内容
print(line);
if __name__ == '__main__':
main();
Random read and
output the file according to the format
#将文件按照既定格式进行输出
NAMES = ("zhangSan","liSi","wangWu");
AGES = (1,2,3);
def main():
with open("E:\\test.txt","a") as file:
for item in range(len(NAMES)):
content = "{name:<10}{age:>4}\n".format(name = NAMES[item],age = AGES[item]);
file.write(content);
if __name__ == '__main__':
main();
Read the data from the specified location of the file
def main():
with open("E:\\test.txt",mode = "r") as file:
# 忽略前15位的数据
file.seek(15);
print("【第二行数据】,当前位置:{},姓名:{},年龄:{}".format(file.tell(),file.read(10).strip(),int(file.read(5))));
print("【第三行数据】,当前位置:{},姓名:{},年龄:{}".format(file.tell(),file.read(10).strip(),int(file.read(5))));
if __name__ == '__main__':
main();
Use seek to relocate the file
def main():
with open("E:\\test.txt",mode = "r") as file:
# 忽略前15位的数据
file.seek(15);
print("【第二行数据】,当前位置:{},姓名:{},年龄:{}".format(file.tell(),file.read(10).strip(),int(file.read(5))));
file.seek(0);
print("【第一行数据】,当前位置:{},姓名:{},年龄:{}".format(file.tell(),file.read(10).strip(),int(file.read(5))));
file.seek(30);
print("【第三行数据】,当前位置:{},姓名:{},年龄:{}".format(file.tell(),file.read(10).strip(),int(file.read(5))));
if __name__ == '__main__':
main();
Directly locate the data you want to read to avoid invalid performance expenses. The disadvantage is that you need to specify the file format to analyze
# 姓名的内容长度为10
NAME_LENGTH = 10;
# 年龄的内容长度为10
READ_LENGTH = 5;
# 保存数据读取的行数
line_count = 0;
def get_age():
# 当前的偏移量
seek_offset = 0;
with open("E:\\test.txt", mode="r") as file:
# 持续进行内容的读取;
while True:
# 读取年龄的位置
file.seek(seek_offset + NAME_LENGTH);
# 读取数据长度
data = file.read(READ_LENGTH);
# 可以获取到data数据
if data:
# 引用全局变量
global line_count;
# 统计的行数追加1
line_count +=1;
# 修改读取偏移量
seek_offset = file.tell();
# 局部返回
yield int(data);
else:
# 结束函数调用
return;
def main():
sum = 0;
for age in get_age():
sum +=age;
print("一共读取了:{}条信息,用户平均年龄为:{}".format(line_count,sum/line_count));
if __name__ == '__main__':
main();
file encoding
def main():
message = "奥特曼-打小怪兽".encode("GBK");
print("编码后的数据类型为:{}".format(type(message)));
# 输出为二进制数组
print(message);
if __name__ == '__main__':
main();
def main():
message = "奥特曼-打小怪兽".encode("GBK");
print("编码后的数据类型为:{}".format(type(message)));
# 输出为二进制数组
print(message);
# 进行解码
print(message.decode("GBK"));
if __name__ == '__main__':
main();
View the current encoding format
# 这个组件需要下载,pip install chardet
import chardet
def main():
message = "奥特曼-打小怪兽".encode();
print(chardet.detect(message));
if __name__ == '__main__':
main();
Output files in the specified format
def main():
# w代表输出 文件不存在时,自动创建,创建成功之后写入数据
with open("E:\\test2.txt", mode="w",encoding="UTF-8") as file:
file.write("奥特曼打小怪兽");
if __name__ == '__main__':
main();
The screenshots and code sources in the article are free python teaching videos from csdn