python检测文件夹中新增文件申请,Python添加、删除、修改和检查文件,对,进行,增删,改查...

file_name = 'passwd.txt'

def read():

with open(file_name, 'r', encoding='utf-8') as f:

data = f.read().split('\n')

data = [i for i in data if i]

return data

def add():

website = input('Input website:')

password = input('Input password:')

with open(file_name, 'a', encoding='utf-8') as f:

f.write('{}\t{}'.format(website, password) + '\n')

def query():

lines = read()

print('查询结果为{}条'.format(len(lines)))

for index, line in enumerate(lines):

print("{}\t{}".format(index + 1, line))

def delete():

input_website = input('Input delete website:')

lines = read()

for line in lines:

read_website = line.split('\t')[0]

if read_website == input_website:

lines.remove(line)

print('{} delete success!'.format(input_website))

break

else:

print('{} delete error!'.format(input_website))

with open(file_name, 'w', encoding='utf-8') as f:

f.write('\n'.join(lines) + '\n')

def modify():

input_website = input('Input modify website:')

lines = read()

for line in lines:

read_website = line.split('\t')[0]

if read_website == input_website:

print('current 【{}】 exist,value is 【{}】'.format(input_website, line))

new_website = input('Input new website:')

new_password = input('Input new password:')

lines[lines.index(line)] = "{}\t{}".format(new_website, new_password)

print('modify success!')

break

else:

print('current 【{}】 not find!,modify failed!'.format(input_website))

with open(file_name, 'w', encoding='utf-8') as f:

f.write('\n'.join(lines) + '\n')

query()

while True:

print('=========================')

print('增加:1')

print('删除:2')

print('修改:3')

print('查询:4')

print('退出:0')

print('=========================')

s = input('Input id:')

if s == '0':

print('已退出')

break

elif s == '1':

add()

elif s == '2':

delete()

elif s == '3':

modify()

elif s == '4':

query()

else:

print('Id input error!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值