规范化从gzip或普通文件获取文件句柄的方式
filehandle的Python项目详细描述
文件句柄
规范化从gzip或普通文件获取文件句柄的方式
打开gzip或常规文件的典型方法
>>>importgzip>>>importos.path>>>files=['/path/to/foo.bar.gz','/path/to/foo.bar']>>>forfinfiles:...root,ext=os.path.splitext(f.replace('.gz',''))...ext=ext[1:]...iff.endswith('.gz'):...handle=gzip.open(f)...else:...handle=open(f)
使用文件句柄
>>>importfilehandle>>>files=['/path/to/foo.bar.gz','/path/to/foo.bar']>>>forfinfiles:...handle,extension=filehandle.open(f)