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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python script to create an empty file
# with current date as name.
  
# importing datetime module
import datetime
  
# datetime.datetime.now() to get 
# current date as filename.
filename = datetime.datetime.now()
  
# create empty file
def create_file():
    # 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()

chevron_right


Output:

01 August 2019.txt


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.