python-使用用户与密码登录验证的检索特定字段ver1(含文件操作知识)

1、功能:用户使用正确用户名与密码登录系统,成功登录后,使用检索功能查找用户信息,查询完成后可以继续查询

1.1、创建相应文件:

root@kali:~/python# ls
contact_list.txt  csvtpy    scanhostport.py  tab.pyc  userinput.py
csvt01            scan1.py  tab.py           test.py
root@kali:~/python# cat contact_list.txt 
tom it m 18912378667 shanghai
jack op m 15876544332 beijin
lilei it m 15325635588 taiwan
flake it w 18900378667 nanchang
alex op m 15833544332 shijiazang
lili lp m 15123635588 hangzhou
mayun xi m 18912345667 ningbo
zhang py w 15876541222 yunnan
lisi op m 15333333333 haikou
wangwu java m 18999999999 shandong
song js w 15000000000 shenzhen
caiqi js w 15555555555 huizhou
root@kali:~/python# vim manage_query.py
root@kali:~/python# python manage_query.py

root@kali:~/python# clear


1.2、代码如下:


root@kali:~/python# cat manage_query.py 
#!/usr/bin/python
# --*-- coding:utf-8 --*--
while True:

input = raw_input('please input your username:') //手动输入用户名

if input == 'xwb': //如果输入的用户名与xwb相同则进入

password = raw_input('please input your password:') //手动输入密码

pd ='123' //设置正确的密码123

while password != pd: //如果输入的密码与123不相同,则进入死循环一直提示用户输入密码

password = raw_input('wrong password,input again:')

else: //如果输入的密码与123相同,则进入

print ('welcome login to taobao!\n') 

while True: //一直处于循环中

match_yes = 0 //设置初始的标识值

input = raw_input('Please input the name who you want to search:') //用户输入需要搜索的特定值,也可以进行模糊查询

contact_file = file('contact_list.txt') //打开所有检索信息的文件,并进行操作

while True and input != '': //一直处于循环中,并且输入空格无法进入

line = contact_file.readline() //读取检索文件每一行信息元素(每次读取依次减少数目)

if len(line) == 0:break //读取到最后line=0时退出循环

if input in line: //如果用户检索的特定元素在文件中,则进入

print 'Match item:%s ' % line //打印检索到的特定元素

match_yes = 1 //此时把初始标识值设置为1

if match_yes == 0:print 'No match item found!!' //如果初始标识值为0时,则进入

else: //如果输入的用户名不与xwb相同,则进入

print 'Sorry, user %s not found' % input


root@kali:~/python# 


1.3、功能运行:

root@kali:~/python# python manage_query.py
please input your username:ssh
Sorry, user ssh not found
please input your username:xwb
please input your password:ew
wrong password,input again:123
welcome login to taobao!


Please input the name who you want to search:tom
Match item:tom itm18912378667shanghai
 
Please input the name who you want to search:jack
Match item:jack opm15876544332beijin
 
Please input the name who you want to search:shanghai
Match item:tom itm18912378667shanghai


2、模块函数readline()运行:

root@kali:~/python# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> c= file('contact_list.txt')
>>> c.readline()
'tom\tit\tm\t18912378667\tshanghai\n'
>>> len(c.readline())
29
>>> c.readline()
'lilei\tit\tm\t15325635588\ttaiwan\n'
>>> len(c.readline())
32
>>> c.readline()
'alex\top\tm\t15833544332\tshijiazang\n'
>>> len(c.readline())
31
>>> c.readline()
'mayun\txi\tm\t18912345667\tningbo\n'
>>> len(c.readline())
30
>>> c.readline()
'lisi\top\tm\t15333333333\thaikou\n'
>>> len(c.readline())
35
>>> c.readline()
'song\tjs\tw\t15000000000\tshenzhen\n'
>>> len(c.readline())
31
>>> c.readline()
'\n'
>>> len(c.readline())
0
>>> 


3、模块file文件操作命令:

3.1、文件操作f = file('xwbtest.txt','w') //写入方式为w是直接覆盖内容

3.1.1、进入python环境创建xwbtest.txt文件

root@kali:~/python# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> f = file('xwbtest.txt','w') //新建文件
>>> f.write('Hello , my name is XWB god!!!!!') //写入内容
>>> f.close //关闭文件

<built-in method close of file object at 0xb75a7f98>
>>> f.close()
>>> 
>>> f.write('Hello , my name is XWB god!!!!!')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> f = file('xwbtest.txt','w') //写入方式为w是直接覆盖内容
>>> f.write('hello world , my name is xwbxwbxbwbxwb god!!!!!!!!')
>>> f.close()
>>> 


在系统可以看到文件行为:

root@kali:~/python# tail -f xwbtest.txt 
Hello , my name is XWB god!!!!!




tail: xwbtest.txt:文件已截断
hello world , my name is xwbxwbxbwbxwb god!!!!!!!!


3.1.2、查看系统是否存在文件xwbtest.txt,与写入文件内容

root@kali:~/python# ls
contact_list.txt  csvtpy           scan1.py         tab.py   test.py       xwbtest.txt
csvt01            manage_query.py  scanhostport.py  tab.pyc  userinput.py
root@kali:~/python# ls
contact_list.txt  csvtpy           scan1.py         tab.py   test.py       xwbtest.txt
csvt01            manage_query.py  scanhostport.py  tab.pyc  userinput.py
root@kali:~/python# cat xwbtest.txt 
Hello , my name is XWB god!!!!!root@kali:~/python# 
root@kali:~/python# tail -f xwbtest.txt 
Hello , my name is XWB god!!!!!

root@kali:~/python# tail -f xwbtest.txt 
Hello , my name is XWB god!!!!!




tail: xwbtest.txt:文件已截断
hello world , my name is xwbxwbxbwbxwb god!!!!!!!!


3.2、文件操作:文件操作file.flush(),可以在操作文件中,自动写入内容,并不会覆盖前面内容

3.2.1、python环境中操作:

>>> import tab
>>> f.
f.__class__(         f.__getattribute__(  f.__reduce_ex__(     f.close(             f.isatty(            f.readinto(          f.truncate(
f.__delattr__(       f.__hash__(          f.__repr__(          f.closed             f.mode               f.readline(          f.write(
f.__doc__            f.__init__(          f.__setattr__(       f.encoding           f.name               f.readlines(         f.writelines(
f.__enter__(         f.__iter__(          f.__sizeof__(        f.errors             f.newlines           f.seek(              f.xreadlines(
f.__exit__(          f.__new__(           f.__str__(           f.fileno(            f.next(              f.softspace          
f.__format__(        f.__reduce__(        f.__subclasshook__(  f.flush(             f.read(              f.tell(              
>>> f = file('xwbtest.txt','w')
>>> f.write('welcome to taobao doo god!!!!!!!!')
>>> f.flush()


>>> f.write('www.taobao.com  wwww.jingdong.com!!!!')
>>> f.flush()
>>> 


>>> f.write('\nni zhenshi niu B ren wu a !!!')
>>> f.flush()
>>> 

>>> f.close()
>>> 

3.2.2、系统操作:

root@kali:~/python# tail -f xwbtest.txt 
Hello , my name is XWB god!!!!!

tail: xwbtest.txt:文件已截断
hello world , my name is xwbxwbxbwbxwb god!!!!!!!!

tail: xwbtest.txt:文件已截断
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!

ni zhenshi niu B ren wu a !!!

root@kali:~/python# cat xwbtest.txt 
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!
ni zhenshi niu B ren wu a !!!root@kali:~/python# 


3.3、文件操作: f = file('xwbtest.txt','a') ,a是append的追加方式,配合f.flush()方式使用及时查看更新内容

3.3.1、python环境操作

root@kali:~/python# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> f = file('xwbtest.txt','a')
>>> f.write('this is python start course!!!!')
>>> f.write('\n today is very nice!!!!')
>>> 

>>> f.flush()
>>> 



3.3.2、系统日志

root@kali:~# cd python/
root@kali:~/python# 
root@kali:~/python# 
root@kali:~/python# tail -f xwbtest.txt 
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!
ni zhenshi niu B ren wu a !!!


root@kali:~/python# tail -f xwbtest.txt 
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!
ni zhenshi niu B ren wu a !!!this is python start course!!!!
 today is very nice!!!!

3.3.3、系统文件操作

root@kali:~/python# cat xwbtest.txt 
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!
ni zhenshi niu B ren wu a !!!root@kali:~/python# 

root@kali:~/python# cat xwbtest.txt 
welcome to taobao doo god!!!!!!!!www.taobao.com  wwww.jingdong.com!!!!
ni zhenshi niu B ren wu a !!!this is python start course!!!!
 today is very nice!!!!root@kali:~/python# 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值