Search code examples
pythonsubprocesspopen

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?


Solution

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