C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: We use the txt extension, but this can be easily changed. We must convert the date.today() result into a string with str.
Python program that uses date.today for file names
from datetime import date
def get_filename_datetime():
# Use current date to get a text file name.
return "file-" + str(date.today()) + ".txt"
# Get full path for writing.
name = get_filename_datetime()
print("NAME", name)
path = "C:\\programs\\" + name
print("PATH", path);
with open(path, "w") as f:
# Write data to file.
f.write("HELLO\n")
f.write("WORLD\n")
Output
NAME file-2017-05-17.txt
PATH C:\programs\file-2017-05-17.txt
Contents (file):
HELLO
WORLD
Note: Please modify the path to one that is relevant for your system. Also, a raw string literal can be used.
String Literals