python 脚本操作文件,运行test.py选择5,init 初始化下,或者在同目录下创建test.txt文件,
#-*-coding:utf-8-*-
import time,os
f="test.txt"
def Select():
contact=file(f)
contact_list=contact.readlines()
sel=raw_input("would you like select All?[y|n]:")
if sel=="y":print file(f).read()
while True:
user_input=raw_input("please input what your select:")
if user_input=="quit":break
for line in contact_list:
if user_input=="":break
if user_input in line:print line
#break
#else:
#print "Sorry,Selecte Faile !"
contact.close()
def Add():
contact=open(f,‘a+‘)
c=contact.readlines()
while True:
user_input=raw_input(‘please input what you would add:‘)
if user_input=="quit":break
if len(user_input)==0:
continue
else:
contact.write(user_input+"\n")
contact.flush()
contact.close()
def Delete():
contact=open(f,‘a+‘)
c=contact.readlines()
while True:
user_input=raw_input(‘please input what you would delete:‘)
if user_input=="":continue
if user_input=="quit":break
for i in c:
if user_input in i:
print i
u_input=raw_input("would you like delete this[y\n]:")
if u_input =="n":break
if u_input =="y":
#print c.index(i)
del c[c.index(i)]
contact.close()
contact=open(f,‘w‘)
for i in c:
contact.write(i)
contact.flush()
contact.close()
print c
def Modf():
contact=open(f,‘a+‘)
c=contact.readlines()
while True:
user_input=raw_input(‘please input modfile[]:‘)
if user_input=="":continue
if user_input=="quit":break
for i in c:
if user_input in i:
print i
m=i.split(‘\t‘)
print m
m_input=raw_input("input modif [name/bumen/tel]:")
if m_input == "name":
new_name=raw_input("please input you new name:")
m[1]=new_name
k=m[0]+‘\t‘+m[1]+‘\t‘+m[2]+‘\t‘+m[3]
print k
c[c.index(i)]=k
if m_input == "bumen":
new_name=raw_input("please input you new bumen:")
m[2]=new_name
k=m[0]+‘\t‘+m[1]+‘\t‘+m[2]+‘\t‘+m[3]
print k
c[c.index(i)]=k
if m_input == "tel":
new_name=raw_input("please input you new tel:")
m[3]=new_name
k=m[0]+‘\t‘+m[1]+‘\t‘+m[2]+‘\t‘+m[3]+‘\n‘
print k
c[c.index(i)]=k
#print c.index(i)
#del c[c.index(i)]
contact.close()
contact=open(f,‘w‘)
for i in c:
contact.write(i)
contact.flush()
contact.close()
def Show():
os.system("cls")
print"Beijing Time"
print time.strftime("%Y-%m-%d %H:%M:%S")
print"**********************"
print" 1.selcete\n 2.Add\n 3.modfile\n 4.delete\n 5.init\n 6.quit"
print"______________________"
def init():
if os.path.isfile(‘./test.txt‘) == True:
print"./test.txt file exists ! "
else:
print"Need to create the file [test000.txt]"
in_input=raw_input("would you like to create the file?[y/n]:")
if in_input == "y":
f=open(‘./test.txt‘,‘w‘)
f.write("1"+‘\t‘+"JK409"+‘\t‘+‘IT‘+‘\t‘+‘99999999‘+‘\n‘)
f.colse()
print "File has been created successfully !"
def Main():
while True:
Show()
user_input=raw_input("plesse input you chioce[1/2/3/4/5/6]:")
if user_input =="":continue
if user_input =="1" or user_input =="selecte":Select()
if user_input =="2" or user_input =="add":Add()
if user_input =="3" or user_input =="modfile":Modf()
if user_input =="4" or user_input =="delete":Delete()
if user_input =="5" or user_input =="init":init()
if user_input =="6" or user_input =="quit":break
if __name__=="__main__":
Main()
本文出自 “jk409” 博客,请务必保留此出处http://jk409.blog.51cto.com/406667/1374467