标签:style blog color io os ar strong 文件 数据
用来处理系统中的文件内容
>>> open(‘/root/file.txt‘) <open file ‘/root/file.txt‘, mode ‘r‘ at 0x7fa536269540> >>> open(‘/root/file.txt‘,‘w‘) <open file ‘/root/file.txt‘, mode ‘w‘ at 0x7fa5362694b0> >>> file(‘/root/file.txt‘,‘a‘) <open file ‘/root/file.txt‘, mode ‘a‘ at 0x7fa536269540>
>>> a=open(‘/root/file.txt‘)
>>> a.readline()
‘1:root:x:0:0:root:/root:/bin/bash\n‘
>>> a.readline()
‘2:bin:x:1:1:bin:/bin:/sbin/nologin\n‘
>>> a.readlines()
[‘3:daemon:x:2:2:daemon:/sbin:/sbin/nologin\n‘,‘4:adm:x:3:4:adm:/var/adm:/sbin/nologin\n‘,‘5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n‘]
>>> a.readline()
‘‘
#!/usr/bin/python file=open(‘/root/file.txt‘,‘r‘) whileTrue: line = file.readline() if len(line)==0:break print line,
#!/usr/bin/python file=open(‘/root/file.txt‘,‘r‘) whileTrue: line = file.readline() if len(line)==0:break newline = line.split(‘:‘) print newline[0],‘--‘,newline[1] [root@likun python_scripts]# python 8file.py root -- x bin -- x daemon -- x adm -- x lp -- x
[root@likun python_scripts]# cat emp.info 01 lk 18610314061 it 02 tom 13539393939 net 03 jack 15093949348 mark 04 james 13892387464 sale
#!/usr/bin/python file=open(‘/root/python_scripts/emp.info‘,‘r‘) emp_ids=[] whileTrue: line=file.readline() if len(line)==0:break line=line.split() emp_ids.append(line[0]) print emp_ids whileTrue: id = raw_input(‘input [id] to show info:‘) file.seek(0) whileTrue: line=file.readline() if len(line)==0: print ‘We dont have id ‘,id break if id == line.split()[0]: print line file.seek(0) id = raw_input(‘input [id] to show info:‘)
标签:style blog color io os ar strong 文件 数据
原文地址:http://www.cnblogs.com/kissdb/p/4009582.html