i have a problem, i want to export a file on a directory that is created using timestamp curent time, but i cant figure it how to do it
createtimestampdir.py
import datetime
import os
def createdir():
now = datetime.datetime.today()
nTime = now.strftime("%d-%m-%Y")
source = 'D:\GIT-files\Automate-Stats\SVN_sample_files\svnfiles'
dest = os.path.join(source+nTime)
if not os.path.exists(dest):
os.makedirs(dest) createdir()
This is the script that i use to create the directory
And this is the main script,
import pandas as pd
from timestampdirectory import createdir
print("-------------------------------------------------------")
print("Output.csv FILE")
print("-------------------------------------------------------")
df = pd.read_csv(r"D:\GIT-files\Automate-Stats\SVN_sample_files\sample_svn_input.txt", sep = '|')
df.columns = ['groups']
df['New Columns'] = df['groups'].apply(lambda x : x.split('=')[0])
df['groups'] = df['groups'].apply(lambda x : ' '.join(x.split('=')[1:]))
df['groups'] = df['groups'].apply(lambda x : x.split(','))
df = df.explode('groups')
df.rename(columns = {'groups':'users', 'New Columns':'Groups'}, inplace = True)
df["users"] = df["users"].str.strip()
print(df)
createdir()
df.to_excel("os.makedirs(dest)\sample_svn_input_update.xlsx", index=False)
what i want, is like after i use the createdir() function to create the dir.
I want to
df.to.excel(in the directory that i created + \sample_svn_input_update.xlsx + index=False)
I tried hundreds of things but idk how to do it if you can help me .
Let's use pathlib
to make this OS agnostic (Unix, Windows, etc) and a little cleaner. It also gives you access to a wide array of path objects.
You want to do the following:
from pathlib import Path, PosixPath
from datetime import datetime
def create_date_directory(path : str) -> PosixPath:
now = datetime.datetime.today()
nTime = now.strftime("%d-%m-%Y")
if not Path(path).joinpath(nTime).exists():
Path(path).joinpath(nTime).mkdir(parents=True)
return Path(path).joinpath(nTime)
### main script ###
from timestampdirectory import create_date_directory
target_dir = create_date_directory(r'D:\GIT-files\Automate-Stats\SVN_sample_files\svnfiles')
### your code ###
df.to_excel(target_dir.joinpath('sample_svn_file.xlsx'),index=False)