I am writing a script that will go through different folders in a shared drive and summarizes some data. This part works fine. However I want to write the results to a csv file. Every time I run the script, I want a folder to be created under a project folder in My Documents, with the folder name as current time stamp, and the csv file name should be like "Analysis- timestamp". My code currently creates the new folder with time stamp as name, but throws an error for the csv file creation part.
from pathlib import Path
import csv
import glob
import os
from datetime import datetime
dt=datetime.now()
timestamp=dt.strftime("%d-%b-%Y (%H:%M:%S.%f)")
resultfolder=Path("C:/Users/xxxx/Documents/Python Scripts/Analysis_results")
csvpath= os.path.join(resultfolder, datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
os.makedirs(csvpath)
path = Path(csvpath)
fpath= (path / timestamp ).with_suffix('.csv')
finals_public = Path("C:/Users/xxxx/Documents/vault1/Reports/Finals_Public")
os.chdir(finals_public)
with fpath.open(mode='w+') as f:
w=csv.writer(f)
w.writerow(["DSFlist"])
for bfr in glob.glob('DSF-*'):
w.writerow([bfr])
Traceback (most recent call last):
File xxxxxx.py:26 in with fpath.open(mode='w+') as f:
File ~\Anaconda3\lib\pathlib.py:1252 in open return io.open(self, mode, buffering, encoding, errors, newline,
File ~\Anaconda3\lib\pathlib.py:1120 in _opener return self._accessor.open(self, flags, mode)
OSError: [Errno 22] Invalid argument: 'C:\Users\xxxx\Documents\Python Scripts\Analysis_results\2022-07-21_10-06-21\21-Jul-2022 (10:06:21.csv'
What am I doing wrong? Why do I get // in the filepath in the error?