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 .
CodePudding user response:
Lets use pathlib
to make this OS agnostic (Unix,Windows et al.. actually is there anything else!?) and a little cleaner. It also gives you access to a wide array of path objects.
If I understand you correctly you want to do the following
- Create a new directory with the current date if it does not exist.
- Write your excel file there.
from pathlib import Path, PosixPath
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)
CodePudding user response:
You can try adding double slashes to the path. Windows usually works with "\" in strings - escape character.
Change createddir func to:-
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)
return dest
Change last 2 lines in Main script to:-
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)
destination_path = createdir()
df.to_excel("{0}\\sample_svn_input_update.xlsx".format(destination_path), index=False)