Python | Create an empty text file with current date as its name
In this article, we will learn how to create a text file names as the current date in it. For this, we can use now() method of datetime module.
The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.
Let’s see a code example and try to understand it better.
import datetime
filename = datetime.datetime.now()
def create_file():
with open (filename.strftime( "%d %B %Y" ) + ".txt" , "w" ) as file :
file .write("")
create_file()
|
Output:
01 August 2019.txt
Last Updated :
02 Aug, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...