简体   繁体   English

在特定目录中创建文件的名称和修改日期列表,并从中创建数据框

[英]Creating a list of names and modification dates of files in a specific directory and make a dataframe out of it

I want to look at all files in a specific directory and get their name and modification date. 我想查看特定目录中的所有文件,并获取它们的名称和修改日期。 I got the modification date. 我知道了修改日期。 What I want to do is get the dates into a dataframe. 我要做的是将日期放入数据框。 So I can work with it. 所以我可以使用它。 I want to get it into something like a pandas dataframe with one column called ModificationTime then the list of all the times. 我想将其放入类似pandas数据框的列表中,其中一列称为ModificationTime,然后列出所有时间。

I am using Jupyter notebooks and Python 3 我正在使用Jupyter笔记本和Python 3

import os
import datetime
import time
import pandas as pd
import numpy as np
from collections import OrderedDict 


with os.scandir('My_Dir') as dir_entries:
    for entry in dir_entries:
        info = entry.stat()
        (info.st_mtime)
        time = (datetime.datetime.utcfromtimestamp(info.st_mtime))
        df = {'ModificationTime': [time]}
        df1 = pd.DataFrame(df)
        print(df1)

#Output is this
            ModificationTime
0 2019-02-16 02:39:13.428990
            ModificationTime
0 2019-02-16 02:34:01.247963
            ModificationTime
0 2018-09-22 18:07:34.829137


#If I print the code in a new cell I only get 1 output

print(df1) 

#Output is this
ModificationTime
0 2019-02-16 02:39:13.428990
df1 = pd.DataFrame([])
with os.scandir('My_Dir') as dir_entries:
    for entry in dir_entries:
        info = entry.stat()
        (info.st_mtime)
        time = (datetime.datetime.utcfromtimestamp(info.st_mtime))
        df = pd.DataFrame({'ModificationTime': [time]})
        df1 = df1.append(df)

This will solve the problem. 这样可以解决问题。 In your code, you create a dataframe but you keep overwriting it so you only get one row in the final dataframe. 在您的代码中,您创建了一个数据框,但是您仍然对其进行覆盖,因此在最终数据框中仅获得一行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM