1. shell 下输入python ,就会出现>>>
>>>help(raw_input) >>> for i in ['a','b']: ... print i, ... a b
2. 写个读取文件每一行的小程序
root@blog-mreald-com:~/test/python_eg# ls 01.py root@blog-mreald-com:~/test/python_eg# python 01.py Enter file name: 01.py #!/usr/bin/python try: filename = raw_input('Enter file name: ') fobj=open(filename,'r') for eachline in fobj: print eachline, fobj.close except IOError, e: print 'file open error:', e
3.写个读取输入,写入文件的程序:
执行过程:
root@blog-mreald-com:~/test/python_eg# python 02.py Enter you file name: filename Enter lines ('.' by itself to quit) . > aa > bb > cc > . Done! root@blog-mreald-com:~/test/python_eg# cat filename aa bb cc
代码:
#!/usr/bin/env python import os ls = os.linesep while True: fname=raw_input('Enter you file name: ') if os.path.exists(fname): print "Error: '%s' already exists" % fname else: break all=[] print "\nEnter lines ('.' by itself to quit) .\n" while True: entry=raw_input('> ') if entry=='.': break else: all.append(entry) fobj = open(fname, 'w') fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.close() print 'Done!'