Open a file (of type BytesIO) with the function

I have the following pieces of code:

this part generates the CSV in memory:

def to_csv(events: list) -> io.BytesIO():
    if not events:
        return None
    bio = io.BytesIO()
    iow = io.TextIOWrapper(bio)
    writer = csv.DictWriter(iow, fieldnames=events[0].keys())
    return bio

this part sends this file to the FTP server:

def send_data(self, bytes: io.BytesIO) -> str:
    filename = f"{time.time()}.csv"
    if not bytes:
        self.__logger.warning("No data to send")
        return None
        self.__ftp.storbinary(f"STOR {filename}", bytes)
    except ftp.all_errors as e:
            "FTP fail data send",
                "type": type(e).__name__,
                "line": e.__traceback__.tb_lineno,
                "file": __file__,
                "detail": e,
        return None

The file that is passed to send_data is closed. How do I reopen it?
I tried to do something like this:

f = open(bytes, "rb")

But an error is returned:

expected str, bytes or os.PathLike object, not _io.BytesIO


Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/operators/", line 171, in execute
    return_value = self.execute_callable()
  File "/home/airflow/.local/lib/python3.7/site-packages/airflow/operators/", line 189, in execute_callable
    return self.python_callable(*self.op_args, **self.op_kwargs)
  File "/opt/airflow/dags/accounting/", line 29, in process_events
    result = Uploader(logger, cfg).send_data(csv_file)
  File "/opt/airflow/dags/accounting/packages/", line 57, in send_data
    self.__ftp.storbinary(f"STOR {filename}", bytes)
  File "/usr/local/lib/python3.7/", line 513, in storbinary
    buf =
ValueError: I/O operation on closed file.

>Solution :

Once to_csv is done using the io.TextIOWrapper, it needs to detach the wrapper from the underlying io.BytesIO object, to stop the wrapper from trying to close the BytesIO object:


Leave a Reply