创建一个文件并往里面写入内容
import os # coding='utf-8' #取得OS的换行符 ls = os.linesep # 获取文件名 while True: fname = raw_input('input file name: ') if os.path.exists(fname): print "ERROR : '%s' already exists!" % fname else: break print fname #获取文件的内容 all = [] print "\nEnter lines('.' by iteslf 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"
读取文件内容并显示出来
# coding='utf-8' # 获取文件名 fname = raw_input('input file name: ') print fname try: fobj = open(fname,'r') except IOError, e: print "**** file open: ",e else: for eachLine in fobj: print eachLine, fobj.close();
获取数值的类型.注意python作为高级语言,却没有对数值进行统一,还是分属于几个类型:整型,浮点型,长整型,复数
# coding='utf-8' # 获取数值的类型 def getNumType(num): print num, 'is', #判定参数一是否为参数二当中的某个元素的实例 if isinstance(num, (int,long,float,complex)): print 'a number of type: ',type(num).__name__ else: print 'not a number at all' getNumType(-68) getNumType(99999999999999999999999999999L) getNumType(98.6) getNumType(-5.2+1.9j) getNumType("1212")