Created
May 7, 2020 02:40
-
-
Save RabeyaMuna/10924a280fc7afc9d108012979df90e1 to your computer and use it in GitHub Desktop.
he file_date function creates a new file in the current working directory, checks the date that the file was modified, and returns just the date portion of the timestamp in the format of yyyy-mm-dd. Fill in the gaps to create a file called "newfile.txt" and check the date that it was modified.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import datetime | |
def file_date(filename): | |
# Create the file in the current directory | |
with open (filename,'w') as file: | |
pass | |
timestamp = os.path.getmtime(filename) | |
c=datetime.datetime.fromtimestamp(timestamp) | |
# Convert the timestamp into a readable format, then into a string | |
# Return just the date portion | |
# Hint: how many characters are in “yyyy-mm-dd”? | |
return ("{}".format(c.strftime("%Y-%m-%d"))) | |
print(file_date("newfile.txt")) | |
# Should be today's date in the format of yyyy-mm-dd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment