This doesn't produce an error and doesn't rename files to 'p1' Not sure why - Thanks
from pathlib import Path
import pandas as pd
quarter_end = pd.to_datetime(pd.datetime.today() - pd.tseries.offsets.QuarterEnd(startingMonth=3)).date()
QE_Dir = quarter_end.strftime("%m.%d.%Y")
QE = quarter_end.strftime("%Y%m%d")
QE_Dir = '03.31.2021'
AnalyticsDirs = ['/home/javert/Dir1/Dir2/ACG']
for AnalyticsDir in AnalyticsDirs:
p1 = Path(AnalyticsDir)
files = [e for e in p1.iterdir() if e.is_file()]
for file in files:
thefile = file.name
thenewfile = re.sub(r'\d{8}', QE, str(thefile))
p2 = p1.joinpath(QE_Dir)
p2.mkdir(parents=False, exist_ok=True)
p3 = p2.joinpath(thenewfile)
file.rename(p3)