Python合并目录

时间:2018-05-16 12:06:44

标签: python file merge concatenation cat

我在目录中有数千个文件,格式为YYYY / MM / DD / HH / MM:

  • 201801010000.txt
  • 201801010001.txt
  • 201801010002.txt

我想保留几个小时,所以我需要每天每小时将60个文件合并为一个。 我不知道如何搜索文件名以获取我想要的60个文件。这就是我写的

def concat_files(path):
    file_list = os.listdir(path)
    with open(datetime.datetime.now(), "w") as outfile:
        for filename in sorted(file_list):
            with open(filename, "r") as infile:
                outfile.write(infile.read())

如何命名文件以保留日期?我现在使用datetime但它覆盖了当前的文件名。使用我的代码我将所有文件合并为一个,我应该将每个%60合并到一个不同的文件中。

3 个答案:

答案 0 :(得分:0)

您可以使用glob来获取所需的文件。它允许您在搜索文件时传入匹配的模式。在下面的最后一行中,它只会找到以'2018010100'开头,有两个字符,以'.txt'结尾的文件

from glob import glob

def concat_files(dir_path, file_pattern):
    file_list = glob(os.path.join(dir_path, file_pattern))
    with open(datetime.datetime.now(), "w") as outfile:
        for filename in sorted(file_list):
            with open(filename, "r") as infile:
                outfile.write(infile.read())

concat_files('C:/path/to/directory', '2018010100??.txt')

答案 1 :(得分:0)

你不是那么远,你只需要交换你的逻辑:

file_list = os.listdir(path)
for filename in sorted(file_list):
    out_filename = filename[:-6] + '.txt'
    with open(out_filename, 'a') as outfile:
        with open(path + '/' + filename, 'r') as infile:
            outfile.write(infile.read())

答案 2 :(得分:0)

试试这个。

file_list = os.listdir(path)
for f in { f[:-6] for f in file_list }:
    if not f:
        continue
    with open(f + '.txt', 'a') as outfile:
        for file in sorted([ s for s in file_list if s.startswith(f)]):
            with open(path + '/' + file, 'r') as infile:
                outfile.write(infile.read())
            #os.remove(path + '/' + file) # optional