The file_date function creates a new file in the current working directory,

Question:

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  ___
  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(___))

print(file_date("newfile.txt")) 

Should be today’s date in the format of yyyy-mm-dd

Asked By: Patel Veer

||

Answers:

You can use fromtimestamp function to deal with timestamp and strftime to format the date object.

from datetime import datetime

timestamp = 1545730073
datetimeobj = datetime.fromtimestamp(timestamp).date()

output

datetime.date(2018, 12, 25)

if you want to format the date

datetimeobj.strftime("%Y-%m-%d")
//'2018-12-25'
Answered By: Rajith Thennakoon
import datetime
from datetime import datetime


def file_date(filename):
  # Create the file in the current directory
  with open(filename,'w'):

    timestamp = os.path.getmtime(filename)
    date = datetime.fromtimestamp(timestamp).date()
  # 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(date))

print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd```
Answered By: aro01
import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  open(filename, 'w')
  timestamp = os.path.getmtime(filename)
  # Convert the timestamp into a readable format, then into a string
  datet = datetime.datetime.fromtimestamp(timestamp).date()
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(datet))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd
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)
  tm = datetime.datetime.fromtimestamp(timestamp).date()
  # Convert the timestamp into a readable format, then into a string
  
  # Return just the date portion 
  return ("{}".format(tm))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd

output date format : 2020-08-20

Answered By: Suhas Raj
import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  with open(filename,'w'):
    timestamp = os.path.getmtime(filename)
    # Convert the timestamp into a readable format, then into a string
    date = datetime.datetime.fromtimestamp(timestamp).date()
    # Return just the date portion 
    # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(date))

print(file_date("newfile.txt")) 
Answered By: Aarju Raj Arya
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)
  # Convert the timestamp into a readable format, then into a string
  time = datetime.datetime.fromtimestamp(timestamp)
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(str(time)[:10]))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd
Answered By: Adel Abu Hashim

Here is way!

    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)
  # Convert the timestamp into a readable format, then into a string
      date = datetime.datetime.fromtimestamp(timestamp).date()
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”?  
      return ("{}".format(date))

    print(file_date("newfile.txt")) 
  # Should be today's date in the format of yyyy-mm-dd
Answered By: SSV 1

2 ways:

1.

st = datetime.datetime.fromtimestamp(timestamp).date()
return ("{}".format(st));
st = datetime.datetime.fromtimestamp(timestamp)  
return ("{}".format(st.strftime("%Y-%m-%d")))
Answered By: PhantomNaz9
import os
from datetime import datetime

def file_date(filename):
    # Create the file in the current directory
    fp = open(filename, 'w')
    timestamp = os.path.getmtime(filename)
    fp.close()
    # Convert the timestamp into a readable format, then into a string
    readable = datetime.fromtimestamp(timestamp)
    string = readable.strftime('%Y-%m-%d %H:%M:%S')

    # Return just the date portion
    # Hint: how many characters are in “yyyy-mm-dd”?
    s1 = slice(10)
    return(string[s1])


print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd

Output will be like 2022-02-12

Answered By: Joeshibha

For context for future readers: this question comes from the Google Professional Certificate in IT Automation. This is a practice question. There are two things the tripped me up when answering this question: I missed the "then into a string" part on line 7, and the {___} in the returned string. Note the given comment that hints at how many characters are in the formatted date.

I’m sure OP has solved this issue considering it’s been several years since the question has been posed; however, the simplest response for this question, given what the course has taught up to this point, is this:

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  with open(filename, "w") as file:
    file.write("")

  timestamp = os.path.getmtime(filename)
  # Convert the timestamp into a readable format, then into a string
  date = str(datetime.datetime.fromtimestamp(timestamp))
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(date[:10]))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd
Answered By: Peter Mahon
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.