简体   繁体   English

如何打印 Python 中各种文件的文件创建日期?

[英]How i can print the file creation date of various files in Python?

I'm trying to print the file date creation of various files.我正在尝试打印各种文件的文件日期创建。

So I've tried use this code:所以我试过使用这段代码:

import os
import datetime

pth = r"my_path"

def listDir(dir):
    fileNames = os.listdir(dir)
    for fileName in fileNames:
        t = os.path.getctime(pth + str(\fileName));
        print('Nombre: ' + fileName + t)

listDir(pth)

But, that don't works.但是,这行不通。 Another problem that i have is that i don't know how to put only in the date the "Y/M/D" characters.我遇到的另一个问题是我不知道如何只在日期中输入"Y/M/D"字符。

I appreciate your help.我感谢您的帮助。

I see a problem here.我在这里看到一个问题。

While using .getctime the string you are passing is not a proper file-path.使用.getctime时,您传递的字符串不是正确的文件路径。 You are concatenating pth with \filename .您将pth\filename连接起来。 This syntactically worng.这在句法上磨损了。

You are missing the \ or other delimiter used in different os for specifying directories.您缺少在不同os中用于指定目录的\或其他分隔符。

You can try the below answer -您可以尝试以下答案 -

import os
import datetime

pth = r"my_path"

def listDir(dir):
    fileNames = os.listdir(dir)
    for fileName in fileNames:
        file_path = os.path.join(pth, fileName)

        ts = os.path.getctime(file_path)        # this returns the creation timestamp.
        dt = datetime.datetime.fromtimestamp(ts)    # this converts the timestamp to datetime object.

        print('Nombre: {0} --> {1}'.format(fileName ,dt.date()))    # dt.date() will return only the date from the datetime object.


listDir(pth)

Notes:笔记:

  1. Use os.path.join for concatenating the paths as it takes care of which OS you are using.使用os.path.join连接路径,因为它负责您使用的操作系统。 We don't have to worry about / or \\ or any other delimiter.我们不必担心/\\或任何其他分隔符。

  2. Convert the date into datetime object as manipulating and dealing with datetime object will be much more easier than dealing with strings .将日期转换为datetime时间 object 因为操作和dealing日期时间 object 将比处理strings容易得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python3,我想获取特定日期范围内所有文件的文件名、文件大小和文件创建日期 - Using python3, I would like to get file name, file size, and file creation date for all files within a specific date range 如何使用Python获取HDFS文件创建日期 - How to get hdfs file creation date with Python 如何在 Python 中重命名文件并保留创建日期 - How to rename a file and preserve creation date in Python 如何根据各种条件打印列表的一部分? - How can I print a part of a list depending on various conditions? Python:按创建日期或文件名中的编号列出目录中的 XML 文件 - Python: List XML Files in Directory by Creation Date or number in file name 如何在特定目录(包括其子目录)中列出所有文件及其大小和创建日期? - How can I list all files with their sizes and date of creation in a specific directory including its sub-directories? 如何在Python中打印文件列表的绝对路径? - How can I print the absolute path of a list of files in Python? 如何在 python 中打印没有 \n 的文本文件 - how can i print text files without \n in python 如何使用 python 以不同的行将文本从一个文件插入另一个文件? - How can I use python to insert text from one file to another in various lines? 如何在Python中获取按创建日期排序的目录中的csv文件列表 - How to get the list of csv files in a directory sorted by creation date in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM