Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python格式文件修改日期_Python_Datetime_Format - Fatal编程技术网

python格式文件修改日期

python格式文件修改日期,python,datetime,format,Python,Datetime,Format,我正在看一个获取文件日期的示例: import os.path, time file = r'C:\Users\...\18.organizer.v3.012119.pdf' print("last modified: %s" % time.ctime(os.path.getmtime(file))) 此返回:上次修改时间:2019年2月9日星期六18:16:48 我在文档中没有看到%s格式与上述格式相同的地方 print( time.ctime(os.path.getmtime(file)

我正在看一个获取文件日期的示例:

import os.path, time 
file = r'C:\Users\...\18.organizer.v3.012119.pdf'
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
此返回:上次修改时间:2019年2月9日星期六18:16:48 我在文档中没有看到%s格式与上述格式相同的地方

print( time.ctime(os.path.getmtime(file))) produces
2019年2月9日星期六18:16:48


如何将os.path.getmtime(文件)格式化为简单的YYYY-MM-DD格式


提前感谢

因为
os.path.getmtime()
返回POSIX时间戳,所以您可以在datetime模块中使用
date.fromtimestamp()
方法

from datetime import date
print(date.fromtimestamp(os.path.getmtime(file)))

具体来说,你在努力解决哪一部分?如何将os.path.getmtime(文件)格式化为简单的YYYY-MM-DD格式?你刚刚重复了你文章中的内容。除了上面所做的之外,我还需要导入其他内容吗?我收到以下错误:NameError:name'date'未定义只要从datetime导入日期导入
os.path
,编辑就应该工作。确保您正确地复制了该行。