0

I have the below code checks for a file in a location stored under variable loads. Given below is the code that does that:

if type == 'file_1':
    current_directory = path.abspath(path.join(__file__, ".."))
    base = os.path.join(current_directory, 'file')
    loads = pd.read_csv(base + f'/output/{name}_{date}.csv', sep= ',')
    resp = make_response(loads.to_csv(index=False))
    resp.headers["Content-Disposition"] = f'attachment; filename={name}_{date}.csv'
    resp.headers["Content-Type"] = "text/csv"
    return resp

I am trying to have this modified such that if file check if the file exists in loads, it returns the csv file else return an empty Dataframe. Could anyone assist how could I modify this, Thanks

dark horse
  • 3,211
  • 8
  • 19
  • 35
  • You can use the following code to check if the file is empty or not empty inside your code and if it returns true, return an empty datarame with the command `return pd.DataFrame()` https://stackoverflow.com/questions/2507808/how-to-check-whether-a-file-is-empty-or-not – Raja Sattiraju Oct 31 '18 at 11:00
  • Depending on what you do further, e.g. check if dataframe is empty... You should reconsider it and return `None` instead, it exists exactly for cases like this. (just adding in case it's XY problem which it seems to be...function that reads data should either return read data or nothing at all, not some random value user would like to have, let them create it themselves) – Purple Ice Oct 31 '18 at 11:35
  • @PurpleIce, thanks for the reply. May be I phrased it incorrectly. What I am trying to do is to check if file exists in the path -- base + f'/output/.. If it does it continues with the lop else returns an empty Dataframe. – dark horse Oct 31 '18 at 11:39

0 Answers0