1

I need to concatenate multiple files that begin with the same name inside a Python program. My idea, in a bash shell, would be to something like

cat myfiles* > my_final_file

but there are two shell operators to use: * and >. This could be easily solved using

subprocess.Popen("cat myfiles* > my_final_file", shell=True)

but everybody says the using shell=True is something you have to avoid for security and portability reasons. How can I execute that piece of code, then?

1 Answer 1

3

You have to expand the pattern in python:

import glob
subprocess.check_call(['cat'] + glob.glob("myfiles*"), stdout=open("my_final_file", "wb"))

or better do everything in python:

with open("my_final_file", "wb") as output:
    for filename in glob.glob("myfiles*"):
        with open(filename, "rb") as inp:
            output.write(inp.read())
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.