设为首页 加入收藏

TOP

Python简单的文件增删改查处理(二)
2014-11-24 02:33:41 来源: 作者: 【 】 浏览:2
Tags:Python 简单 文件 删改 处理
文件覆盖源文件,实现删除功能
with open('contact_list.txt','r') as ff:
with open('contact_list.txt.new','w') as gg:
for line in ff.readlines():
if user_del_input not in line:
gg.write(line)
if user_del_input in line:
print "\033[32;1mDelete item:\033[0m"
print "\033[32;1m------------------\033[0m"
print " %s " %line
_counter += 1 # 计数器,判断输入值命中次数
print "\033[32;1m------------------\033[0m"
print "\033[32;1mDeleted successful!\033[0m"
if _counter == 0:
print 'nothing delete!'
shutil.move('contact_list.txt.new','contact_list.txt')
# 退出删除
user_del_input_quit = raw_input("\033[32;1mpress Q for quit or press any key to continue \033[0m").strip()
if user_del_input_quit == 'Q':
break


# 查询
elif item == 's':
while True:
match_yes = 0
#输入判断,忽略空格输入,加入颜色
user_select_input = raw_input("\033[32;1mplease input sth to search:\033[0m ").strip()
contact_file = file (staff_list)
if len(user_select_input) == 0:
print "empty input,try again!"
else:
while True:
line = contact_file.readline()
if len(line) == 0:
break
if user_select_input in line:
match_yes = 1
print line
else:
pass
if match_yes == 0 :
print "No match item found"
# 退出查询
user_select_input_quit = raw_input("\033[32;1mpress Q for quit or press any key to continue \033[0m").strip()
if user_select_input_quit == 'Q':
break

# 修改
elif item == 'u':
while True:
# 输入为空以及匹配查询内容判断
user_update_input_from = raw_input("\033[32;1mplease search sth to update: \033[0m").strip()
update_match = 0
update_file = file(staff_list).readlines()
for n_ in range(len(update_file)):
if user_update_input_from in update_file[n_]:
update_match = 1
else:
pass
if update_match == 0:
print "No match item found"
elif len(user_update_input_from) == 0:
print "empty input,try again!"
else:
# 将匹配到的字符修改为新字符
while True:
user_update_input_to = raw_input("\033[32;1mupdate %s to what \033[0m " %(user_update_input_from)).strip()
if len(user_update_input_to) == 0:
print "empty input,try again!"
else:
for line_ in fileinput.input(staff_list,inplace = 1,backup='.bak'):
line_ = line_.replace(user_update_input_from,user_update_input_to)
print line_
# 打印修改字符的行
print "\033[32;1mupdate item:\033[0m"
output_ = commands.getoutput("diff contact_list.txt contact_list.txt.bak|grep '^>.*' | sed 's/^>//g'")
print "\033[32;1m---------------------------\033[0m"
print output_
print "\033[32;1m---------------------------\033[0m"
print "\033[32;1mupdate successfully!\033[0m"
# 删除空行
del_blank_in = open('contact_list.txt','r')
del_blank_out = open('contact_list_new.txt','w')
lines = del_blank_in.readlines()
for blank in lines:
if blank.split():
del_blank_out.writelines(blank)
del_blank_in.close()
del_blank_out.close()
# 覆盖原文件
shutil.move('contact_list_new.txt','contact_list.txt')
break
# 退出更新
user_update_input_quit = raw_input("\033[32;1mpress Q for quit or press any key to continue \033[0m").strip()
if user_update_input_quit == 'Q':
break


# 退出
elif item == 'q':
print 'bye!'
sys.exit()

else:
pr
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java中的foreach性能差别 下一篇简单的Linux脚本程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: