Home > Back-end >  Python; Pandas read_csv() not working when file has less than ~200 rows
Python; Pandas read_csv() not working when file has less than ~200 rows

Time:07-01

When I download my CSV file from the FTP server and it contains ~200-500 rows my application works just fine, but when I download a CSV file from the FTP server with only ~1-200 rows I get the error: No columns to parse from file

The CSV contains the headers: "example1";"example2";"example3";"example4"

with rows looking like: "8481832";2022-30-06";GFBG21";4,00

    with open(csv_filename, 'wb') as file:
        ftp.retrbinary(f"RETR {csv_filename}", file.write)
        file_data = pd.read_csv(csv_filename, sep=';', header=0)

Both files are fully identical to each other, just a different amount of rows

Does anyone know if this is a bug, or am i doing something wrong?

CodePudding user response:

I'm surprised it works for files with line count > 200. You use a context manager and you don't wait the file is closed before reading it. It probably works for other files because the system writes the buffer on disk. You have to de-indent your last line to wait the file be closed properly:

Try:

    with open(csv_filename, 'wb') as file:
        ftp.retrbinary(f"RETR {csv_filename}", file.write)
    file_data = pd.read_csv(csv_filename, sep=';', header=0)
  • Related