获取文本文件中的子目录列表并使用新的子目录名称附加该 txt 文件

Getting sub directories list in a text file and append that txt file with new subdirectory name

我正在尝试编写一个脚本,它将一个目录中的所有子目录列出到一个 txt 文件中。

此脚本将 运行 通过 cron 作业每 1 小时执行一次,以便我可以附加到之前 运行 中已创建的 txt 文件并添加新的子目录名称。

例如:

/Directory
  /subdir1
  /subdir2
  /subdir3

txt.file 应该有以下列:

subdir_name     timestamp   first_filenamein_thatSUBDIR
subdir1         2015-23-12  abc.dcm
subdir2         2014-23-6   ghj.nii
.
.
.

我知道使用 os.listdir 获取目录列表,但不知道如何解决这个问题,因为我想用新名称编写相同的 txt 文件。知道我应该如何在 python 中做到这一点吗?

编辑:使用 os.listdir 我得到子目录名称但不是时间戳。另一个问题是如何创建两列,一列带有子目录名称,另一列带有时间戳,如上所示?

在@Termi 的帮助下,我使这段代码正常工作:

import time
import os
from datetime import datetime
parent_dir = '/dicom/'

sub_dirs = os.walk(parent_dir).next()[1]

with open('exam_list.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]
    for sub in sub_dirs[1:len(sub_dirs)]:
    sub = sub + '/0001'
        latest_modified = os.path.getctime(os.path.join(parent_dir,sub))
        if sub not in present_dirs and time.time() - latest_modified < 4600 :
            created = datetime.strftime(datetime.fromtimestamp(latest_modified),'%Y-%d-%m')
            file_in_subdir = os.walk(os.path.join(parent_dir,sub)).next()[2][1]
            f.write("%s\t%s\t%s\n"%(sub,created,file_in_subdir))

这段代码,当在 python 终端上输入时,可以很好地处理所有创建的子变量,file_in_subdir 持有一些值,但是,无法将其写入提到的文件中代码的开头。

如果文件写入有问题,我也尝试使用以下代码:

with open('./exam_list.txt','a+') as f:
    f.write("%s\t%s\n"%(sub,file_in_subdir))

以上两行按照我的意图正确创建了文件..

无法指出错误是什么。

with open('some.txt', 'a') as output:
    output.write('whatever you want to add')

使用 'a' 作为参数打开文件会将您写入的所有内容附加到文件末尾。

您可以使用 os 包中的 walk。 比 listdir 好多了。 您可以阅读更多相关信息 here 恩例:


import os
from os.path import join, getctime

with open('output.txt', 'w+') as output:

    for root, dirs, files in os.walk('/Some/path/'):
        for name in files:
            create_time = getctime(join(root, name))
            output.write('%s\t%s\t%s\n' % (root, name, create_time))

要获取父目录中的直接子目录,请使用 os.walk('path/to/parent/dir').next()[1]

os.walk().next() 给出列表列表 [current_dir, [sub-dirs], [files] ] 所以 next()[1] 给出子目录

使用 'a+' 打开文件将允许您读取和附加到文件。然后存储文件中已有的子目录

with open('dirname.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]

现在为每个子目录检查它是否已经存在于列表中,如果不存在,将其添加到文件中。如果您每小时执行一次,您甚至可以使用 getctime

检查最近一小时内创建(或在 linux 系统中修改)的新文件
time.time() - os.path.getctime(os.path.join(parent_dir,sub)) < 3600

现在对于任何新的子目录使用 os.walk('path/to/subdir').next[2] 并获取里面的文件名

import time
import os
from datetime import datetime
parent_dir = '/path/to/parent/directory'

sub_dirs = os.walk(parent_dir).next()[1]

with open('dirname.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]
    for sub in sub_dirs:

        latest_modified = os.path.getctime(os.path.join(parent_dir,sub))

        if sub not in present_dirs and time.time() - latest_modified < 3600 :

            created = datetime.strftime(datetime.fromtimestamp(latest_modified),'%Y-%d-%m')
            file_in_subdir = os.walk(os.path.join(parent_dir,sub)).next()[2][0]
            f.write("%s\t%s\t%s\n"%(sub,created,file_in_subdir))