Search code examples
pythoncsvexport-to-excelfilesize

how to get the size of csv file line by line?is thas possible using python


I am reading a csv file without pandas library,i need to find out the number of bytes of each row.So i can ad all the bytes and determine the file size and will do some restriction based on that.

file="Emptycsv.csv"
checkfilesize(file)

def checkfilesize(file):
 file=b''join(file).split(b'\n')
 count=len(file)

Count will print the number of rows.i need to find out the file size,i know os.path.getsize will do..But while reading that csv file row by rows/column or columns if it reached 109997897kb,i need to terminate.So request you to help me the find the bytes of data


Solution

  • Something along the lines of this should work for getting the bytes per row/line:
    (The +1 is for the newline)

    import os
    
    total_bytes = -1
    
    with open("test.csv") as file_in:
        for line in file_in:
            bytes_on_this_line = len(line) + 1
            total_bytes += bytes_on_this_line
            
    print(total_bytes)
    
    print(os.path.getsize("test.csv"))
    

    Output:

    15
    15