2

I am writing a cleaning function to clean my excel files to analyse them. I get a excel sheet everyday which is named for example:

"tourniquets_27.07.2022_raw.xls"

I get such a sheet everyday and so the date changes to that day. I now change the dates manually but I would love to make this automatic. I also save the file as follow: "tourniquets_28.07.2022_cleaned.xls". The dates need to bne changed here as well.

I tried the following code but it gives an error, I tried other things as well but this seems to be the most close to what I want.

import pandas as pd
import os 
from pathlib import Path
from datetime import date

#assuming the files are in the directory:
folder = Path("C:/Users/JHA4/Desktop/Code/Tourniquets/Cleaned")
date_string = f"tourniquets_{date.today().month}.{date.today().day}.{date.today().year}_raw.xlsx"
xlsx_file = folder.glob(date_string)

#read in data
df = pd.read_excel(io=next(xlsx_file)

#Save it back to excel with a new name=
df.to_excel(io=next(xlsx_file))

Hope that I asked everything clearly! Thank you in advance.

Jishdk
  • 21
  • 2
  • So, you just want to rename the file with present date in it? or you want to perform some operations as well? – KD_ Aug 09 '22 at 11:02
  • I left the operations out of this question because it is out of the scope. I want to rename the files to different dates ranging from 25-07-2022 till now and the future. So not only todays date but also days before I run the code. – Jishdk Aug 09 '22 at 11:11
  • 1
    Does this answer your question? [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python) – Laurent Aug 13 '22 at 14:40

0 Answers0