cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to add a current date as suffix while using copy?

databicky
Contributor II

how to add a current date after filename suffix while copy from the dbutils like report20221223.xlsx

โ€‹

dbutils.fs.cp('dbfs://temp/balancing/report.xlsx','abfss://con@adls/provsn/result/report.xlsx',True)

โ€‹

i need to add the current date in the file like report20220123.xlsx while copying from source to target location

how can we add a current date after the suffix in filename while using dbutils.โ€‹

1 ACCEPTED SOLUTION

Accepted Solutions

Chaitanya_Raju
Honored Contributor

@Mohammed sadamuseanโ€‹ hope the below code might help you,

from datetime import datetime
date_value = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File_{date_value}.csv' 
 
dbutils.fs.cp(src,trgt)

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

View solution in original post

4 REPLIES 4

Aviral-Bhardwaj
Esteemed Contributor III

This will give you a little bit more idea

import time
 
timestr = time.strftime("%Y%m%d-%H%M%S")
 
print(timestr)

Now you can use formator and implement this solution

Thanks

Aviral

AviralBhardwaj

Chaitanya_Raju
Honored Contributor

@Mohammed sadamuseanโ€‹ hope the below code might help you,

from datetime import datetime
date_value = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File_{date_value}.csv' 
 
dbutils.fs.cp(src,trgt)

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

how can we get the data without hyphenโ€‹

You can try like below

from datetime import datetime
datevalue = datetime.now().strftime("%Y%m%d")
 
src = 'dbfs:/FileStore/Test/File.csv'
trgt = f'dbfs:/FileStore/Test/File{datevalue}.csv' 
 
dbutils.fs.cp(src,trgt)
 
 

Happy Learning!!

Thanks for reading and like if this is useful and for improvements or feedback please comment.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group