简体   繁体   English

如何在特定目录(包括其子目录)中列出所有文件及其大小和创建日期?

[英]How can I list all files with their sizes and date of creation in a specific directory including its sub-directories?

I'm writing a code in Python to list all files with their sizes and date of creation in a specific directory including its sub-directories.我正在用 Python 编写代码,以列出特定目录(包括其子目录)中的所有文件及其大小和创建日期。 The code I ended up with works only for the current directory, but not for a specific directory.我最终得到的代码仅适用于当前目录,而不适用于特定目录。 If I replace the value of the variable folder by a specific directory it comes up with an error.如果我将变量文件夹的值替换为特定目录,则会出现错误。

Below is my code:下面是我的代码:

import os, sys, time

folder = "C:\ENTD261"
listOfFiles = ""
for root, dirs, files in os.walk(folder):
      for list in files:
       file_size = os.path.getsize(list)
       createDate = time.ctime(os.path.getctime(list))
       listOfFiles = list, "Size: %.1f bytes"%file_size, "Created date: " + createDate
       print(listOfFiles)

For specific directory, you need to join the root path with the file path for full path.对于特定目录,您需要将根路径与文件路径连接为完整路径。

for root, dirs, files in os.walk(folder):
for list in files:
    list=os.path.join(root,list) # joining root and the file name for full path
    file_size = os.path.getsize(list)
    createDate = time.ctime(os.path.getctime(list))
    listOfFiles = list, "Size: %.1f bytes"%file_size, "Created date: " + createDate
    print(listOfFiles)

暂无
暂无

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

相关问题 如何删除目录中的所有文件,并保持子目录完整 - How to delete all files in a directory, keeping sub-directories intact 通过 python 从目录及其子目录中的 txt/srt 文件中删除特定的空白行 - Removing specific blank lines from txt/srt files inside a directory and its sub-directories by python 列出给定目录及其子目录中存在和不存在的文件 - List files that exist and do not exist in a given directory and its sub-directories 尝试列出目录的所有子目录时键入错误 - Type error when trying to list all the sub-directories of a directory 在 PySpark 中指定时间戳之后创建的目录及其子目录中的文件计数 - Counting files in a directory and its sub-directories created after a specified timestamp in PySpark 在特定目录及其子目录中,找到所有扩展名为.tmp的文件夹 - In a particular directory and its sub-directories, find all the folders ending with .tmp extension 如何移动不同子目录中的文件? - how to move files in different sub-directories? Python 2.7如何列出目录和所有子目录中的文件? - Python 2.7 How to list files in a directory AND all sub directories? 如何在子目录中合并文件并在多个子目录上执行此功能 - How to merge files within a sub-directory and perform this function on mutliple sub-directories 在目录和子目录中找到所有文件,并提供目录的路径 - find all files indirectory and sub-directories and provide the path from directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM