我需要以 "yyyy-mm-dd" 格式创建的文件的日期,但我不明白 datetime.datetime.timstamp() returns 是什么
I need date for the file I created in the format "yyyy-mm-dd", but I don't understand what datetime.datetime.timstamp() returns
我到目前为止编写的代码包含在下面。我在
将变量分配给的印象
datetime.datetime.fromtimestamp() 会生成一个列表,所以我创建了
我的 Python 代码如下。我希望我的功能达到 return 的时间
我首先将我的文件创建为格式字符串的子字符串
“yyyy-mm-dd”。如果您能帮助我使这段代码正常工作,我将不胜感激。
import os
import datetime
def file_date(filename):
# Create the file in the current directory
with open(filename, "x") as file1:
pass
timestamp = os.path.getmtime(file1)
# Convert the timestamp into a readable format, then into a string
list1 = datetime.datetime.fromtimestamp(timestamp)
str1 = "-".join(list1)
# Return just the date portion
# Hint: how many characters are in “yyyy-mm-dd”?
return ("{str1[0:9]}".format(str1)
print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd
list1 是一个类型为 datetime.datetime 的对象,使用
将其格式化为字符串
time_in_string = list1.strftime('%Y-%m-%d')
没有。 datetime.datetime.fromtimestamp
是 datetime
实例(日期时间 class 的对象)的命名构造函数(构造函数“构造”class 的实例)。这意味着调用 datetime.datetime.fromtimestamp(timestamp)
将为您提供与该时间戳相对应的日期时间对象,而不是列表。当您打印日期时间实例时,您会得到 datetime.datetime(2021, 1, 13, 13, 15, 1, 270342)
.
要将datetime
实例解析为格式化时间字符串“yyyy-mm-dd”,您可以使用
list1.strftime("%Y-%m-%d")
其中 %Y 是年份 %m 是零填充月,%d 是零填充日
你也可以试试这个功能-
>>> import os
>>> import platform
>>> from datetime import datetime
>>>
>>> def file_creation(path_to_file):
... if platform.system() == 'Windows':
... dt = os.path.getctime(path_to_file)
... else:
... stat = os.stat(path_to_file)
... try:
... dt = stat.st_birthtime
... except AttributeError:
... dt = stat.st_mtime
... return datetime.fromtimestamp(dt).strftime("%Y-%m-%d")
...
>>>
>>> file_creation('test.sh')
'2018-06-18'
getmtime 和 getctime 采用路径而不是文件对象
(datetime.fromtimestamp(timestamp)) 的类型是 'datetime.datetime',不是列表
import os
import time
from datetime import datetime
def file_date(filename):
with open(filename, 'w') as file1:
pass
path = os.path.abspath(filename)
timestamp = os.path.getctime(path)
date_created = datetime.fromtimestamp(timestamp)
str_DT = date_created.strftime('%Y-%m-%d')
return str_DT
print(file_date("file2.txt"))
我到目前为止编写的代码包含在下面。我在 将变量分配给的印象 datetime.datetime.fromtimestamp() 会生成一个列表,所以我创建了 我的 Python 代码如下。我希望我的功能达到 return 的时间 我首先将我的文件创建为格式字符串的子字符串 “yyyy-mm-dd”。如果您能帮助我使这段代码正常工作,我将不胜感激。
import os
import datetime
def file_date(filename):
# Create the file in the current directory
with open(filename, "x") as file1:
pass
timestamp = os.path.getmtime(file1)
# Convert the timestamp into a readable format, then into a string
list1 = datetime.datetime.fromtimestamp(timestamp)
str1 = "-".join(list1)
# Return just the date portion
# Hint: how many characters are in “yyyy-mm-dd”?
return ("{str1[0:9]}".format(str1)
print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd
list1 是一个类型为 datetime.datetime 的对象,使用
将其格式化为字符串time_in_string = list1.strftime('%Y-%m-%d')
没有。 datetime.datetime.fromtimestamp
是 datetime
实例(日期时间 class 的对象)的命名构造函数(构造函数“构造”class 的实例)。这意味着调用 datetime.datetime.fromtimestamp(timestamp)
将为您提供与该时间戳相对应的日期时间对象,而不是列表。当您打印日期时间实例时,您会得到 datetime.datetime(2021, 1, 13, 13, 15, 1, 270342)
.
要将datetime
实例解析为格式化时间字符串“yyyy-mm-dd”,您可以使用
list1.strftime("%Y-%m-%d")
其中 %Y 是年份 %m 是零填充月,%d 是零填充日
你也可以试试这个功能-
>>> import os
>>> import platform
>>> from datetime import datetime
>>>
>>> def file_creation(path_to_file):
... if platform.system() == 'Windows':
... dt = os.path.getctime(path_to_file)
... else:
... stat = os.stat(path_to_file)
... try:
... dt = stat.st_birthtime
... except AttributeError:
... dt = stat.st_mtime
... return datetime.fromtimestamp(dt).strftime("%Y-%m-%d")
...
>>>
>>> file_creation('test.sh')
'2018-06-18'
getmtime 和 getctime 采用路径而不是文件对象
(datetime.fromtimestamp(timestamp)) 的类型是 'datetime.datetime',不是列表
import os
import time
from datetime import datetime
def file_date(filename):
with open(filename, 'w') as file1:
pass
path = os.path.abspath(filename)
timestamp = os.path.getctime(path)
date_created = datetime.fromtimestamp(timestamp)
str_DT = date_created.strftime('%Y-%m-%d')
return str_DT
print(file_date("file2.txt"))