Python | Create empty text file with current date as name



The datetime module provides classes for manipulating dates and times in both simple and complex ways. Although date and time arithmetic is supported, the focus of the implementation is on efficiently extracting attributes for formatting and processing the output.

Let`s take a look at the sample code and try to understand it better.

# Python script to create an empty file
# with the current date as the name.

 
# import date and time module

import datetime

 
# datetime.datetime.now () to get
# current date as filename.

filename = datetime.datetime.no w ()

 
# create empty file

def create_file ():

# The function creates an empty file

#% d - date,% B - month,% Y - year

with open (filename.strftime ( "% d% B% Y" ) + ". txt" , "w" ) as file :

file .write ("")

  
Driver code
create_file ()

Exit:

 01 August 2019.txt