0

I have a folder in dropbox with path:

C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs

and I want to import the last modified .xlsx file. I use the code:

list_of_files = glob.glob(r'C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs/*') 
latest_file = max(list_of_files, key=os.path.getctime)

the latest_file , prints:

C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs\~$CD_LG_Results - 2020-11-27 - mean - v2.xlsx

which is in fact the latest file, and therefore the file I want to import. However, when I try to import the file:

x = pd.read_excel(latest_file)

I get the following error: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\x0cSophocl'

I don't know how to fix the error, but I also think that fixing this should be fairly straight forward. Any help on this will be appreciated. Thank you.

sophocles
  • 13,593
  • 3
  • 14
  • 33
  • Possibly related to this: https://stackoverflow.com/questions/16504975/error-unsupported-format-or-corrupt-file-expected-bof-record. Does read_csv work? – Ryan Deschamps Dec 02 '20 at 13:46
  • 1
    i think the file is a temp file, ~$ this indicates it. close the file restart the PC and Try again, ~$ Should not come. it is temp file created when spreadsheet is open – Vignesh Dec 02 '20 at 13:52
  • may be you have to delete that temp file in the dropbox – Vignesh Dec 02 '20 at 13:53
  • Thanks @VigneshRajendran, I think that's what the issue. I was able to resolve it with using: ```x = pd.read_excel(f"{latest_file}".replace('~$',''),sheet_name=2)``` – sophocles Dec 02 '20 at 14:17
  • Happy about that @sophods – Vignesh Dec 03 '20 at 05:23

1 Answers1

0

Question was solved after removing ~$ (temp-file) using the code below:

x = pd.read_excel(f"{latest_file}".replace('~$',''),sheet_name=2)

Thanks.

sophocles
  • 13,593
  • 3
  • 14
  • 33