处理数据时候,需要得到数据所在和行号,使用enumerate时便捷的方法: file = open('file.txt','r') for (num,value) in enumerate(file): print "line num is: ",num,"content:",value file.close()
print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno PDB,哈哈http://docs.python.org/library/pdb.html import fileinput fileinput.filename() import srcinfo print ('warning: %s: %d: %s' %(srcinfo.file(), srcinfo.line(), x)) 绝对路径
一.说明 1.python进程池进行多进程运行时,如果有错误,该进程会直接跳过,并且不会打印错误信息. 2.如果需要了解到进程内的错误信息,此时就需要通过捕获异常来输出错误信息了. 二.具体方法如下: 法一: 注:此方法不会打印错误代码具体位置 a = [1, 2, 3] try: b = a[5] except Exception as ee: print(ee) s = ee # 如果想在except语句外使用ee,需要用变量储存 # 输出ee: # list index out of ra
用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容 #coding=utf-8 class handle_file(object): def __init__(self,file_path): self.file_path=file_path @classmethod def write_file(cls,file_path,content): with open(file_path,'a') as