python - 多个文件到单个文件的符号链接(symbolic link)

标签 python ubuntu unix logging symlink

我有一个场景,我想将多个文件的日志重定向到同一个符号链接(symbolic link)。
我假设三个文件 file1.log file2.logfile3.logfile1.log

This is file 1
file2.log
This is file 2
file3.log
This is file 3
现在我想创建所有这个文件的符号链接(symbolic link)到 file.log使得 file.log 的内容是
This is file 1
This is file 2
This is file 3
这是动态发生的。即如果file1.log的内容然后修改例如 This line append to file1.log然后 file1.log应该显示
This is file 1
This is file 2
This is file 3
This line append to file1.log
有没有这样做的标准方法?我已经在这里停留了很长一段时间。
我正在使用 ubuntu 18.04python 3.6.9 - - - - - - - - - - - - - - -编辑 - - - - - - - - - - ---------------
这不是我拥有的所有文件,例如 20 个名称为 file*.log 的文件并且可以有多个文件,如 f1X.log f2X.log需要重定向到f1.log , f2.log

最佳答案

from itertools import chain
from pathlib import Path
from time import sleep

log_path_list = [p for p in Path('.').glob('*.log') if p.name != 'file.log']
log = Path('file.log')

files = []
for log_path in log_path_list:
    files.append(log_path.open())
lines = chain(*map(lambda f: (line for line in f),files))
with log.open('w') as flog:
     for line in lines:
         flog.write(line)
     flog.flush()
     try:
         while True:
            oneline = ""
            for f in files:
                 f.seek(0, 2)
                 oneline = f.readline()
                 if not oneline:
                     continue
                 flog.write(oneline)
            if oneline:
                 flog.flush()
            else:
                 sleep(0.3)
     except Exception as e:
         print(e)
         for f in files:
             f.close()
这不是符号链接(symbolic link),但它会将所有日志文件中的所有内容输出到file.log然后将任何新行追加到 file.log .
只要file.log,python 进程就应该在那里停留。需要有更新。
我不认为这是一个聪明的解决方案,但如果这是你的唯一方法,那么你就有了一个解决方案的开始。

关于python - 多个文件到单个文件的符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64747305/

相关文章:

python - 霍夫曼编码树遍历

Python tar.add 文件但省略父目录

python - 如何在 open-office-writer 中给代码上色?

Ubuntu "make"错误 : hg: not found

bash - 字符串文件路径用作命令参数时不起作用

unix网络进程

python - 为什么要将 python 列表转换为 numpy 数组?

mysql - 无法通过 Ubuntu 服务器连接到远程 MySQL 数据库

Docker 设置,无法克隆存储库,因为容器名称已被占用

c - 子进程中的dup2是否更改了父进程中的fd表?