课程目标
- Python内置的文件读写操作
- 通过OS模块进行文件和文件夹操作
- 通过OS模块进行路径操作
Python内置的文件读写操作
• open() 打开或者创建一个文件
格式:open(‘文件路径’,‘打开模式’)
打开模式:r w a 等模式 具体请参考手册
• close() 关闭文件 • read() 读取文件 • readline() 读取一行文件 • readlines() 将文件中的内容读取到序列当中
• write() 写入文件 • writelines() 将序列写入文件中
通过OS模块进行文件和文件夹操作
• 使用该模块必须先导入模块:
import os
• os模块中的部分函数:
getcwd()-- 获取当前的工作目录
chdir()-- 修改当前工作目录
listdir()-- 获取指定文件夹中的
mkdir()-- 创建一个目录/文件夹
rmdir()-- 移除一个目录(必须是空目录)
rename()—修改文件和文件夹名称
stat() 获取文件的相关 信息
exit() 推出当前执行命令,直接关闭当前操作
• os.path os中的一个子模块,操作非常多
通过OS模块进行路径操作
• abspath() 将一个相对路径转化为绝对路径
• basename() 获取路径中的文件夹或者文件名称
• dirname() 获取路径中的路径部分(出去最后一部分)
• join()将2个路径合成一个路径
• split()将一个路径切割成文件夹和文件名部分
• getsize()获取一个文件的大小
• isfile()检测一个路径是否是一个文件
• isdir()检测一个路径是否是一个文件夹
• getctime() 获取文件的创建时间
• getmtime()获取文件的修改时间
• getatime()获取文件的访问时间
• exists()检测指定的路径是否存在
open()
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
# 读取文件按照字节
# f = open('./a.txt','r')
# content = f.read(3)
# print(content)
# f.close()
#
# #按照行来读取
# f = open('./a.txt','r')
#
# content = f.readline()
# while len(content) > 0:
# print(content)
# content = f.readline()
# f.close()
#
# print('='*60)
#
# # 文件的写入操作
# f = open('./b.txt','w')
# f.write('hello cjj\n')
# f.close()
#
#
# f = open('./b.txt','w')
# f.write('hello cjj\n')
# a = ['hello java\n','hello php\n''hello c++\n']
# f.writelines(a)
# f.close()
# 引入python模块os
import os
#文件复制函数
# def copy_file(file1,file2):
# # '''
# # 文件复制函数
# # :param file1:
# # :param file2:
# # :return:
# # '''
# # # 打开源文件和目标文件
# # f1 = open(file1,'rb')
# # f2 = open(file2,'wb')
# # # 循环读取写入,实现复制内容
# # content = f.readline()
# # while len(content) > 0:
# # f2.write(content)
# # content = f.readline()
# # f1.close()
# # f2.close()
# 目录复制函数通过os模块
import os
def copy_path(dir1,dir2):
# 获取被复制目录中的所有文件信息,dir1是真实存在的
dlist = os.listdir(dir1)
# 创建新目录
os.mkdir(dir2)
# 遍历所有文件,并执行文件复制
for f in dlist:
# 为遍历的文件添加目录路径
file1 = os.path.join(dir1,f) #源
file2 = os.path.join(dir2,f) #目标
if os.path.isfile(file1):
copy_file(file1,file2) #调用我们已经写好的文件复制函数
# 判断是否是目录(文件夹)
if os.path.isdir(file1):
copy_path(file1,file2) #递归调用自己,来实现子目录的复制
copy_path('./cjj','./newfile')
小结
edu.csdn.net
• Python文件操作
1.文件读取
2.文件写入
• Python文件目录操作
1.文件复制
2.目录复制
实战:学生信息管理系统
运用所学Python知识,完成一个在线学员信息管理系统
• 数据临时存放在变量列表中
• 实现学生信息的添加,删除和查询操作
通过案例实战,锻炼和巩固Python基础知识
'''
需求
学员信息管理系统
1、学员信息数据源
2、查看学员信息
3、添加学员信息
4、删除学员信息
5、退出系统
6、界面和交互
'''
#1.学员信息数据源
stu_list = [
{'name':'zhangsan','age':20,'classid':'python01'},
{'name':'list','age':28,'classid':'pthonr02'},
{'name':'zhaoliu','age':22,'classid':'pthonr03'}]
# 2.查看学员信息
def show_stus_info():
'''
:return:
'''
if len(stu_list) == 0:
print('='*20,'没有学员信息','='*20)
return
print('|{0:<5}|{1:<10}|{2:<5}|{3:<10}|'.format('sid','name','age','classid'))
print('-'*40)
for i,stu_dict in enumerate(stu_list):
print('|{0:<5}|{1:<10}|{2:<5}|{3:<10}|'.format(i+1,stu_dict['name'],stu_dict['age'],stu_dict['classid']))
# 3.添加学员信息
def add_stu(name,age,classid):
stu_dict = {}
stu_dict['name'] = name
stu_dict['age'] = age
stu_dict['classid'] = classid
stu_list.append(stu_dict)
# 4。删除学员信息
def del_stu(sid):
sid_int = int(sid)
stu_list.pop(sid_int-1)
# 5.退出系统
def loginOut():
pass
# 6.界面和交互
while True:
# 输出一个初始界面
print('='*12,'学员管理系统','='*12)
print('{:1} {:13} {:15}'.format(' ','1. 查看学员信息','2. 添加学员信息'))
print('{:1} {:13} {:15}'.format(' ', '3. 删除学员信息', '4. 退出系统'))
print('='*40)
key = input('请输入对应的操作:')
# 根据输入操作值,执行对应操作
if key == '1':
print('='*12,'学员信息浏览','='*12)
show_stus_info()
input('按回车继续:')
elif key == '2':
print('=' * 12, '添加学员信息', '=' * 12)
name = input('请输入姓名:')
age = input('请输入年龄')
classid = input('请输入班级号')
add_stu(name,age,classid)
show_stus_info()
input('按回车继续:')
elif key =='3':
print('=' * 12, '删除学员信息', '=' * 12)
show_stus_info()
sid = input('请输入要删除学员的sid')
del_stu(sid)
show_stus_info()
input('按回车继续:')
elif key == '4':
loginOut()
print('=' * 12, '再见', '=' * 12)
break
else:
print('=' * 12, '操作无效', '=' * 12)
# #测试查看学员信息
# show_stus_info()
# 测试添加学员信息
# add_stu('yh',18,'python04')
# show_stus_info()
# # 测试删除学员信息
# del_stu(2)
# show_stus_info()