1

On my Mac, I am using the following command to call g++ in python:

subprocess.run(['g++', './rmc-output/*.cpp', '-w', '-o', 'executable'], check=True,
               stderr=PIPE, stdout=PIPE, shell=True)

however, I get the following error while the rmc-output folder is not empty.

clang: error: no such file or directory: './rmc-output/*.cpp'

Am I missing something?

user1723583
  • 553
  • 1
  • 6
  • 18

2 Answers2

2

shell=True won't expand wildcards when arguments are put in a list. Quickfix: use a string:

subprocess.run('g++ ./rmc-output/*.cpp -w -o executable', check=True,
           stderr=PIPE, stdout=PIPE, shell=True)

quick but dirty, so a better solution:

  • drop shell=True (avoid whenever possible, security issues, lazy command lines...)
  • use glob to compute files using python, not the shell

like this:

import glob
subprocess.run(['g++'] + glob.glob('./rmc-output/*.cpp') +['-w', '-o', 'executable'], check=True,
           stderr=PIPE, stdout=PIPE)

note that Windows versions of g++ have internal wildcard expansion to make up for the fact that Windows "shell" hasn't. Would have worked on Windows probably.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

*.cpp is a shell wildcard pattern. Since you're specifying the command arguments directly, wildcard filename expansion never happens, and it's using *.cpp as a literal filename.

See this answer https://stackoverflow.com/a/9997093/494134 for more details.

John Gordon
  • 29,573
  • 7
  • 33
  • 58