需求:
1 、从info. txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info. txt文件
2 、能增查改员工工资
3 、增、改员工工资用空格分隔
4 、实现退出功能
import os, sys
def check ( ) :
with open ( 'info.txt' , 'r' ) as f:
ret = [ i. strip( ) . split( ) for i in f]
return ret
def check1 ( name) :
res = check( )
for i in res:
if name == i[ 0 ] :
print ( i)
def add ( name, salary) :
f = open ( 'info.txt' , 'a' , encoding= 'utf8' )
f. write( name+ ' ' )
f. write( salary+ '\n' )
f. close( )
def change ( name, salary) :
with open ( 'info.txt' , 'r' ) as f1:
for i in f1:
if name == i. strip( ) . split( ) [ 0 ] :
f2 = open ( 'info.txt1' , 'a' , encoding= 'utf8' )
f2. write( name+ ' ' )
f2. write( salary+ '\n' )
f2. close( )
else :
f3 = open ( 'info.txt1' , 'a' , encoding= 'utf8' )
f3. write( i. strip( ) . split( ) [ 0 ] + ' ' )
f3. write( i. strip( ) . split( ) [ 1 ] + '\n' )
f3. close( )
os. renames( 'info.txt' , 'info.txt.bak' )
os. renames( 'info.txt1' , 'info.txt' )
os. remove( 'info.txt.bak' )
def dell ( name) :
res1 = check( )
res2 = [ i[ 0 ] for i in res1]
if name in res2:
for i in res1:
if name == i[ 0 ] :
continue
else :
f = open ( 'info.txt2' , 'a' , encoding= 'utf8' )
f. write( i[ 0 ] + ' ' )
f. write( i[ 1 ] + '\n' )
f. close( )
os. renames( 'info.txt' , 'info.txt.bak' )
os. renames( 'info.txt2' , 'info.txt' )
os. remove( 'info.txt.bak' )
print ( '已删除' )
else :
print ( '不存在改员工信息' )
msg = '''
'0':'退出系统'
'1':'查找所有员工信息',
'2':'增加员工信息',
'3':'修改员工信息',
'4':'删除员工信息',
'5':'查找特定员工信息',
'''
print ( msg)
while True :
choice = input ( '请选择:' )
if choice == '0' :
sys. exit( '退出' )
elif choice in [ '1' , '2' , '3' , '4' , '5' ] :
if choice == '1' :
res = check( )
print ( res)
else :
name = input ( '请输入名字:' ) . strip( )
if choice == '2' :
salary = input ( '请输入工资:' ) . strip( )
add( name, salary)
if choice == '3' :
salary = input ( '请输入工资:' ) . strip( )
change( name, salary)
if choice == '4' :
dell( name)
if choice == '5' :
check1( name)
else :
print ( '输入的选项有误,请重新选择' )
continue