0

I have been searching and although I find long and complicated (many features I do not need) on how to just simply have python have linux use the cat function to concatenate files to a single file.

From my reading apparently subprocess is the way to do this. Here is what I have but obviously does not work :(

subprocess.call("cat", str(myfilelist[0]), str(myfilelist[1]), str(myfilelist[2]), str(myfilelist[3]), ">", "concatinatedfile.txt"])

the above assumes:

myfilelist[]

the above list has 4 filenames + paths as a list; for example one item in the list is "mypath/myfile1.txt"

I would take non subprocess (but simple) methods also

0

3 Answers 3

5

since > is a shell function you need to do shell=True

subprocess.call("echo hello world > some.txt",shell=True) ... works in windows at least

alternatively you can do somethign like

with open("redirected_output.txt") as f:
    subprocess.call("/home/bin/cat some_file.txt",stdout=f)
Sign up to request clarification or add additional context in comments.

Comments

4

If you want to use cat and redirection > you must call a shell, for example via system:

from os import system
system("cat a.txt b.txt > c.txt")

However you must pay attention to code injection.

3 Comments

coolz! this worked faster. so what is the difference of using subprocess then?
good answer(+1) i just did the same thing with subprocess
here you are running a shell so it is slower and more prone to security risks
2

See this question. His solution seems to be concise and easy to understand, I'll post it here:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

4 Comments

Thanks Stephan. THis does work, and I had seen it. It is just that it is CPU intensive when you have text files each over 10k or more lines :( I wanted to use the faster cat function of linux.
this seems to be some really optimized concatenation @StudentOfScience
I just ran it with my big files, took 1 min 34 sec. When I use cat manually (minus me typing the code) it take 24 sec. that is why. Not that I do not have an additional 1 min and 10 sec to give, but just as a learning opportunity to use subprocess, or python to linux I asked the question. Thx.
this would help you with your learning opportunity to use subprocess for concatenation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.