Python进阶篇:文件系统的操作
通过一个例子来熟悉文件的基本操作:创建文件,读取文件,修改文件,删除文件,重命名文件,判断文件是否存在
'''
编写可供查询的员工信息表--学号 姓名 年龄 班级
1. 提供格式化查询接口
2. 允许用户添加或者删除员工信息
'''
import os
employee_dir = 'employee_list'
index=employee_dir+"/index" if(not os.path.exists(employee_dir)):
os.mkdir(employee_dir,777) def add_student(id,name,age,_class):
local_file = employee_dir+"/"+str(id)
if(os.path.isfile(local_file)):
print("id: %s 已经存在,请重新添加" %(id))
else:
f=open(local_file,"w")
f.write(name+"\t"+str(age)+"\t"+_class)
f.close()
f=open(index,"a")
f.write(str(id)+"\t"+name+"\n")
f.close()
print("id=%s添加成功" %id) def select_student(**kwargs):
for key in kwargs.keys():
if(key=='id'):
local_file = employee_dir+"/"+str(kwargs[key])
if(not os.path.isfile(local_file)):
print("不存在id:",kwargs[key])
else:
f=open(local_file,"r")
print(kwargs[key],f.read())
elif(key=='name'):
if(not os.path.isfile(index)):
print("不存在name:",kwargs[key])
else:
i=0
f=open(index,"r")
for line in f.readlines():
id,name = line.strip().split("\t")
if(name == kwargs[key]):
i=i+1
select_student(id=id)
if(i==0):
print("不存在name:",kwargs[key])
else:
print("无效的获取方式,只能通过id和name来获取!") def modify_student(id,name,age,_class):
local_file = employee_dir+"/"+str(id)
if(os.path.isfile(local_file)):
os.remove(local_file)
rm_line(index,id)
add_student(id,name,age,_class)
print("id=%s修改成功" %id)
else:
print("id: %s 不存在,请先添加" %(id)) def delete_student(**kwargs):
for key in kwargs.keys():
if(key=='id'):
local_file = employee_dir+"/"+str(kwargs[key])
if(not os.path.isfile(local_file)):
print("不存在id:",kwargs[key],",无法删除")
else:
os.remove(local_file)
rm_line(index,kwargs[key])
print("删除id=%s成功" %kwargs[key])
elif(key=='name'):
if(not os.path.isfile(index)):
print("不存在name:",kwargs[key],",无法删除")
else:
i=0
f=open(index,"r")
for line in f.readlines():
id,name = line.strip().split("\t")
if(name == kwargs[key]):
i=i+1
delete_student(id=id)
if(i==0):
print("不存在name:",kwargs[key],",无法删除")
else:
print("无效的删除方式,只能通过id和name来删除!") def rm_line(file,id):
if(not os.path.isfile(file)):
print("不存在文件:",file)
else:
f= open(file,"r")
of = open(file+".tmp","w")
for line in f.readlines():
localid,localname = line.strip().split("\t")
if(str(id)==localid):
pass
else:
of.write(line,)
f.close()
of.close()
if(os.path.isfile(file+".tmpt")): os.remove(file+".tmpt")
os.rename(file,file+".tmpt")
os.rename(file+".tmp",file)
Python进阶篇:文件系统的操作的更多相关文章
- python进阶篇
python进阶篇 import 导入模块 sys.path:获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到. import sys ...
- Python 进阶篇
作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这 ...
- Python进阶篇四:Python文件和流
摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...
- python基础篇 08 文件操作
本节主要内容:1. 初识⽂件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操作⽅法9. ⽂件的修改以及另⼀ ...
- python 进阶篇 迭代器和生成器深入理解
列表/元组/字典/集合都是容器.对于容器,可以很直观地想象成多个元素在一起的单元:而不同容器的区别,正是在于内部数据结构的实现方法. 所有的容器都是可迭代的(iterable).另外字符串也可以被迭代 ...
- Python进阶学习_连接操作Redis数据库
安装导入第三方模块Redis pip3 install redis import redis 操作String类型 """ redis 基本命令 String set(n ...
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- python 进阶篇 函数装饰器和类装饰器
函数装饰器 简单装饰器 def my_decorator(func): def wrapper(): print('wrapper of decorator') func() return wrapp ...
- python 进阶篇 python 的值传递
值传递和引用传递 值传递,通常就是拷贝参数的值,然后传递给函数里的新变量,这样,原变量和新变量之间互相独立,互不影响. 引用传递,通常是指把参数的引用传给新的变量,这样,原变量和新变量就会指向同一块内 ...
随机推荐
- Python练习-基于授权方式包装list之与根儿哥必有一战
# 编辑者:闫龙 # 基于授权定制自己的列表类型,要求定制的自己的__init__方法, # 定制自己的append:只能向列表加入字符串类型的值 # 定制显示列表中间那个值的属性(提示:proper ...
- java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)
最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...
- 【bzoj题解】1008 越狱
题目描述 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. 输入 输入两个整 ...
- 从Python到Web开发
基础部分: 1-编程基础及Python环境部署 2-Python基础语法-内存管理-运算符-程序控制 3-Python内置结构-列表 4-Python数据类型之元组-字符串 5-python的封装与结 ...
- django Rest Framework----APIView 执行流程 APIView 源码分析
在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...
- ASP.NET中Literal,只增加纯粹的内容,不附加产生html代码
页面代码 <div style="float: right; color: #666; line-height: 30px; margin-right: 12px;" id= ...
- 开启nginx目录文件列表功能
ngx_http_autoindex_module 此模块用于自动生成目录列表,ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时 ...
- css边框内凹圆角,解决优惠券的边框问题
关于css边框内凹圆角,找了好久才找到的 <html <head> <title>无标题页</title> <style> body{ backg ...
- Jenkins的授权和访问控制
默认的Jenkins不包含任何的安全检查,任何人可以修改Jenkins设置,job和启动build等.显然地在大规模的公司需要多个部门一起协调工作的时候,没有任何安全检查会带来很多的问题. 在系统管理 ...