I want to cat txt files from a folder and cat results should be shown in terminal (Obviously). I have tried using listdir() it but it doesn't work. Required some help!
Asked
Active
Viewed 1,847 times
-2
-
1Why do you not simply use `cat`? – mkrieger1 Feb 10 '18 at 21:27
-
Possible duplicate of [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – Barmar Feb 10 '18 at 21:28
-
[`listdir`](https://docs.python.org/3/library/os.html#os.listdir) returns a list of filenames. Why did you expect it to print the file contents? – mkrieger1 Feb 10 '18 at 21:28
-
using cat with each file name is a tough task when I have 50 plus text files. So I wanted to open all and cat results in terminal. – captainfreaked Feb 10 '18 at 21:29
-
Please include in your question the code you have tried to use, and please explain in more detail in what way it did "not work". – mkrieger1 Feb 10 '18 at 21:29
-
You know you can use `cat *.txt`? – mkrieger1 Feb 10 '18 at 21:29
2 Answers
0
a simple implementation uses glob
to generate absolute paths to files with .txt
extension, in a loop which reads the file and prints it on standard output:
import glob,sys
for filepath in sorted(glob.glob("path/to/directory/*.txt")):
with open(filepath) as f:
sys.stdout.write(f.read())
using fileinput
allows to read all the files, and line by line, probably less memory intensive and shorter:
import glob,sys,fileinput
for f in fileinput.input(sorted(glob.glob("path/to/directory/*.txt"))):
sys.stdout.write(f)
note that sorted
is better to ensure that the files are processed in deterministic order (some filesystems don't respect that order)
sys.stdout.write(f)
still writes line by line, but as suggested by comments you could improve performance and not use so much memory by using shutil.copyfileobj(f,sys.stdout)

Jean-François Fabre
- 137,073
- 23
- 153
- 219
-
thanks for the reply I have one more doubt that what if the files are not in .txt extension but are grep able files. How would it work then? – captainfreaked Feb 10 '18 at 21:45
-
1Got it for grep we will only use * and trim the extension :) thanks for the help! – captainfreaked Feb 10 '18 at 21:49
-
1For memory concerns, without worrying about line-by-line behaviors, you can use [`shutil.copyfileobj`](https://docs.python.org/3/library/shutil.html#shutil.copyfileobj), replacing `sys.stdout.write(f)` with `shutil.copyfileobj(f, sys.stdout)` (it performs a block by block copy with a fixed size block, so it's not concerned with line endings at all). – ShadowRanger Jun 28 '18 at 00:39
-
@ShadowRanger excellent suggestion. Just when I thought I wouldn't learn anything by answering that :) well, I know `copyfileobj` just wouldn't have the idea of using it – Jean-François Fabre Jun 28 '18 at 08:19
0
Just use the command in terminal "cat *"
Or if you want to do it in python:
import os
allTextFileContents = os.popen("cat *").read();
print(allTextFileContents);
You can also do other things with the contents since its stored as a variable!