通过一个例子来熟悉文件的基本操作:创建文件,读取文件,修改文件,删除文件,重命名文件,判断文件是否存在

 '''
编写可供查询的员工信息表--学号 姓名 年龄 班级
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进阶篇:文件系统的操作的更多相关文章

  1. python进阶篇

    python进阶篇 import 导入模块 sys.path:获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到. ​ import sys ...

  2. Python 进阶篇

    作者:武沛齐 出处:http://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这 ...

  3. Python进阶篇四:Python文件和流

    摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...

  4. python基础篇 08 文件操作

    本节主要内容:1. 初识⽂件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操作⽅法9. ⽂件的修改以及另⼀ ...

  5. python 进阶篇 迭代器和生成器深入理解

    列表/元组/字典/集合都是容器.对于容器,可以很直观地想象成多个元素在一起的单元:而不同容器的区别,正是在于内部数据结构的实现方法. 所有的容器都是可迭代的(iterable).另外字符串也可以被迭代 ...

  6. Python进阶学习_连接操作Redis数据库

    安装导入第三方模块Redis pip3 install redis import redis 操作String类型 """ redis 基本命令 String set(n ...

  7. go语言之进阶篇文件常用操作接口介绍和使用

    一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...

  8. python 进阶篇 函数装饰器和类装饰器

    函数装饰器 简单装饰器 def my_decorator(func): def wrapper(): print('wrapper of decorator') func() return wrapp ...

  9. python 进阶篇 python 的值传递

    值传递和引用传递 值传递,通常就是拷贝参数的值,然后传递给函数里的新变量,这样,原变量和新变量之间互相独立,互不影响. 引用传递,通常是指把参数的引用传给新的变量,这样,原变量和新变量就会指向同一块内 ...

随机推荐

  1. Shell基础-通配符

    * - 通配符,代表任意字符 ? - 通配符,代表一个字符 # - 注释 | - 分隔两个管线命令的界定 ; - 连续性命令的界定 ~ - 用户的根目录 $ - 变量前需要加的变量值 ! - 逻辑运算 ...

  2. JS设计模式——8.桥接模式

    桥接模式的用途 在实现API的时候,桥接模式非常有用. 在设计一个JavaScript API的时候,可以用这个模式来弱化它与使用它的类和对象之间的耦合. 示例:事件监听器 桥接模式最常见和实际的应用 ...

  3. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  4. Runtime - 消息发送原理

    Runtime - 消息发送原理. Objective-C运行时的核心就在于消息分派器objc_msgSend,消息分派器把选择器映射为函数指针,并调用被引用的函数. 要想理解objc_msgSend ...

  5. [HBase]region split流程

    1. 简介 HBase 的最小管理单位为region,region会按照region 分裂策略进行分裂. 基于CDH5.4.2 2. 总览

  6. 经典面试题:js继承方式下

    上一篇讲解了构造函数的继承方式,今天来讲非构造函数的继承模式. 一.object()方法 json格式的发明人Douglas Crockford,提出了一个object()函数,可以做到这一点. fu ...

  7. 使用html+css+js实现魔性的舞蹈

    使用html+css+js实现魔性的舞蹈,让我们燥起来!!! 效果图: 代码如下,复制代码即可使用: <!DOCTYPE html> <html > <head> ...

  8. AdvStringGrid 垂直居中 、水平居中

    官网faq,解答: 结果:

  9. 向SQL Server 现有表中添加新列并添加描述.

    注: sql server 2005 及以上支持. 版本估计是不支持(工作环境2005,2008). 工作需要, 需要向SQL Server 现有表中添加新列并添加描述. 从而有个如下存储过程. (先 ...

  10. Effective STL 学习笔记 Item 18: 慎用 vector<bool>

    vector<bool> 看起来像是一个存放布尔变量的容器,但是其实本身其实并不是一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> ...