How to best manage file either gz or not?(如何最好地管理gz或非gz文件?)

Hi I wonder if there is a better way in terms of code readability and repetition.

I have a large file that do not fit in memory. The file is either compressed .gz or not.

If it is compressed I need to open it using gzip from standard lib.

I am not sure the code I ended up is the best way to deal with that situation.

import gzip
from Path import pathlib

def parse_open_file(openfile):
    """parse the content of the file"""
    return

def parse_file(file_: Path):
    if file.suffix == ".gz":
        with gzip.open(file_, 'rb') as f:
            parse_open_file(f)
    else:
        with open(file_, 'rb') as f:
            parse_open_file(f)

Solution:

One way to handle this is to assign either open or gzip.open to a variable, depending on file type, then use that as an ‘alias’ in the with statement. For example:

if file.suffix == ".gz":
  myOpen = gzip.open
else:
  myOpen = open

with myOpen(file_, 'rb') as f:
  parse_open_file(f)
————————

嗨,我想知道在代码可读性和重复性方面是否有更好的方法。

我有一个大文件,无法存储在内存中。文件要么被压缩。gz与否。

如果它是压缩的,我需要使用标准库中的gzip打开它。

我不确定我最终的代码是否是处理这种情况的最佳方式。

import gzip
from Path import pathlib

def parse_open_file(openfile):
    """parse the content of the file"""
    return

def parse_file(file_: Path):
    if file.suffix == ".gz":
        with gzip.open(file_, 'rb') as f:
            parse_open_file(f)
    else:
        with open(file_, 'rb') as f:
            parse_open_file(f)

解决方法:

处理这个问题的一种方法是分配open或gzip。根据文件类型打开一个变量,然后在with语句中将其用作“别名”。例如:

if file.suffix == ".gz":
  myOpen = gzip.open
else:
  myOpen = open

with myOpen(file_, 'rb') as f:
  parse_open_file(f)