Skip to content

Instantly share code, notes, and snippets.

@pandorica-opens
Created October 26, 2020 11:48
Show Gist options
  • Select an option

  • Save pandorica-opens/3fcf85437010c758262e09a2f56e2bd7 to your computer and use it in GitHub Desktop.

Select an option

Save pandorica-opens/3fcf85437010c758262e09a2f56e2bd7 to your computer and use it in GitHub Desktop.
The 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.
def file_date(filename):
# Create the file in the current directory
# For mknod the root priveleges are required
os.mknod(filename)
timestamp = os.path.getmtime(filename)
# Convert the timestamp into a readable format, then into a string
time = datetime.datetime.fromtimestamp(timestamp)
time = str(time)
# Return just the date portion
# Hint: how many characters are in “yyyy-mm-dd”?
return ("{:.10s}".format(time))
print(file_date("newfile.txt"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment