Estimated reading time: 2 minutes
Ever wondered how to go about checking if a file is empty?
A problem you may come across in Data Analytics that when you are importing a file as outlined in this post Python – How to import data from files is how do we know if the files are empty or not before import?
In the world of data, there are several reasons to check :
- You have an automated process relying on the import not been empty.
- A process that preceded you receiving the file did not work.
- The amount of time and effort to investigate the problem causes undue work to fix.
The nuts and bolts of it all
Here we have a video that looks at different scenarios on how to bring in files. The following functionality appears in this video:
- os.path.getsize – This looks to get the file size attached to the file. * Please see note below
- pd.read_csv
- pd.read_excel
The add on bits
*One note about os.path.getsize, which we found:
- It only works in the logic provided if the size of the file is zero KB.
- CSV and XLSX files even though they where created empty, when saved had a file size greater than zero.
- TXT files, when created empty and saved, had a file size of zero.
Hope this video helps explain further how empty files can be checked in python before they are processed.
Thanks!
Data Analytics Ireland