David
David

Reputation: 1267

Trying to avoid shell=True in a Python subprocess

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?

Upvotes: 1

Views: 73

Answers (1)

Daniel
Daniel

Reputation: 42748

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())

Upvotes: 3

Related Questions