简体   繁体   English

如何从 python 脚本中捕获多个文件的内容

[英]How to cat contents of multiple files from a python script

I would like to cat the contents of the files generated from a python script.我想对从cat脚本生成的文件的内容进行分类。 Is it possible to do that in a simple one line command?是否可以在一个简单的单行命令中做到这一点? For example I would like to have something like:例如,我想要类似的东西:

cat <(python test.py) # doesnt work as I want to

where test.py produces multiple filenames like so (separated by new line)其中 test.py 产生多个像这样的文件名(由新行分隔)

file1.txt
file2.txt
file3.txt

I would like to basically do我想基本上做

cat file1.txt
cat file2.txt
cat file3.txt

Basically reading the contents of the filename produced by the script.基本上是读取脚本产生的文件名的内容。 Assume the python script can generate hundreds/thousands of filenames.假设 python 脚本可以生成数百/数千个文件名。

Though this may seem to work尽管这似乎可行

cat $(python test.py)

But the problem is it seems to wait until the whole python test.py is completed, before it performs any cat .但问题是它似乎要等到整个python test.py完成后,才能执行任何cat Basically it doesnt seem to cat the contents of the filename as soon as it gets a filename.基本上,它似乎不会在获得文件名后立即捕获文件名的内容。 Where as然而

cat <(python test.py)

cat the filename as it gets it, unfortunately, it just prints the filename but not the content of the filename. cat获取文件名,不幸的是,它只打印文件名而不是文件名的内容。

You could use sed你可以使用sed

$ sed 's/^/cat /e' <(python3 test.py)

This will add cat in front of each filename before executing the command.这将在执行命令之前在每个文件名前添加cat

^ - This will anchor the find to the start of each line ^ - 这会将查找锚定到每行的开头

cat - cat will replace the anchor at the start of each line cat - cat 将替换每行开头的锚点

e - This tells sed to execute the commmand that resulted from the substitution, in this case cat file1.txt e - 这告诉 sed 执行由替换产生的命令,在本例中为cat file1.txt

I think you need to create the STDOUT with these files in your script:我认为您需要在脚本中使用这些文件创建 STDOUT:

For example test.py例如test.py

import os
for i in range(0,5):
  name="file" + str(i) +".txt"
  f = open(name, "a")
  f.write("Hello\n")
  f.close()
  print(name) 

Something like this:像这样的东西:

$ python test.py
file0.txt
file1.txt
file2.txt
file3.txt
file4.txt

$ cat $(python test.py)
Hello
Hello
Hello
Hello
Hello

If you want the cat to work on the fly, it's not so simple because in the single line bash we have to wait for the previous command to finish.如果你想让在飞行中工作,这并不是那么简单,因为在单行 bash 中我们必须等待前面的命令完成。 But you can try to do something like this:但是你可以尝试做这样的事情:

$ python test.py > /dev/null &
$ watch -n60 'find ./  -maxdepth 1 -type f -mmin -1 -exec cat {} \;'

Other than using sed , as in this answer , you should consider xargs :除了使用sed ,如本答案所示,您应该考虑xargs

python3 test.py | xargs -i cat "{}"

This is more robust than cat since, unlike the sed solution, it works well with many unconventional characters such as '*' , '?'这比cat更强大,因为与sed解决方案不同,它适用于许多非常规字符,例如'*''?' , and ' ' (but not with '\n' ). , 和' ' (但不是'\n' )。

A minor change to your python script, it is trivial to make it work well with filenames with '\n' characters as well.对您的 python 脚本稍作更改,使其与带有'\n'字符的文件名也能正常工作是微不足道的。 The change in the python scripts will use '\0' instead of '\n' to separate the filenames (make sure to flush stdout after each such filename). python 脚本中的更改将使用'\0'而不是'\n'来分隔文件名(确保在每个此类文件名之后刷新标准输出)。 Then use xargs with the -0 argument:然后将xargs-0参数一起使用:

python3 test.py | xargs -0 -i cat "{}"

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

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