问题
I am trying to download a file from an FTP server in memory, transform it to a dataframe but also return it as bytes. Code as follows:
import io
import pandas as pd
from ftplib import FTP
ftp_connection.cwd(ftp_folder)
download_file = io.BytesIO()
ftp_connection.retrbinary('RETR ' + str(file_name), download_file.write)
download_file.seek(0)
file_to_process = pd.read_csv(download_file, engine='python')
After searching on Stack Overflow, the suggestion was to just read the io stream:
download_file.read()
ValueError: I/O operation on closed file.
Not sure what to try next, without writing the file somewhere and reading it again as bytes.
回答1:
read_csv
probably closes the "file". So read it before you call read_csv
:
download_file.seek(0)
contents = download_file.read()
download_file.seek(0)
file_to_process = pd.read_csv(download_file, engine='python')
来源:https://stackoverflow.com/questions/58320638/read-ftp-file-contents-in-python-and-use-it-at-the-same-time-for-pandas-and-dire