1

I'm working on a little script that downloads a csv file from a ftp server, and when downloaded i want to do some calulations on each row before i write it to a new file.

i have the ftp part working, but i cant figure out how to use the BytesIO data as csv data.

Im using the code below but that prints 0 rows.

Any help or push in the right direction would be very appreciated.

from ftplib import FTP
import io

ftp = FTP(FTP_SERVER)
ftp.login(FTP_USER, FTP_PASSWORD)
csv_data = io.BytesIO()
ftp.retrbinary('RETR ' + FTP_FILE, csv_data.write)

csv_reader = csv.reader(csv_data, delimiter=",")
for row in csv_reader:
    print(row)
VA79
  • 474
  • 3
  • 7
  • Try: https://ftputil.sschwarzer.net/ instead. – alvas Mar 21 '23 at 12:17
  • Thanks @alvas! I'll have a look at that, but i rather not use any external modules if possible. Any way i can get this working without it!? – VA79 Mar 21 '23 at 12:28

1 Answers1

3
  1. After the download, the read/write pointer of the BytesIO is at the end. You have to seek it back to the beginning before you read it.

  2. Additionally, you need to convert the downloaded bytes to text.

csv_data.seek(0)
csv_data = io.TextIOWrapper(csv_data, encoding='utf-8')

See also Read CSV files to Pandas dataframe from FTP with Python ftplib.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks! I thought that too and tried that but that results in the error: "_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)" – VA79 Mar 21 '23 at 13:52
  • Did you try `StringIO` instead of `BytesIO`? – Martin Prikryl Mar 21 '23 at 13:55
  • I did but that cant be passed to ftp.retrbinary() – VA79 Mar 21 '23 at 14:37
  • Oh man, i tried both of those, just not together! Thanks so much for helping me out here... this was driving me mad! But my script is working like a charm now! :) – VA79 Mar 21 '23 at 19:06