file_date函数在当前工作目录中创建一个新文件


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")) 

应为今天的日期,格式为yyyy-mm-dd

您可以使用fromtimestamp函数来处理时间戳,使用strftime来格式化日期对象。

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

输出

datetime.date(2018, 12, 25)

如果要格式化日期

datetimeobj.strftime("%Y-%m-%d")
//'2018-12-25'
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

输出日期格式:2020-08-20

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```
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")) 
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

这是方法!

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

两种方式:

1.

st = datetime.datetime.fromtimestamp(timestamp).date()
return ("{}".format(st));
st = datetime.datetime.fromtimestamp(timestamp)  
return ("{}".format(st.strftime("%Y-%m-%d")))
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

输出将类似于2022-02-12

对于未来读者的上下文:这个问题来自谷歌IT自动化专业证书。这是一个练习题。在回答这个问题时,有两件事让我感到困惑:我错过了";然后变成字符串";第7行的部分,以及返回字符串中的{___}。请注意给定的注释,该注释提示格式化日期中有多少字符。

考虑到这个问题已经提出好几年了,我相信OP已经解决了这个问题;然而,考虑到课程到目前为止所教授的内容,对这个问题最简单的回答是:

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
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

相关内容

最新更新