Search code examples
pythonbashcommandexeinline

Inner quotes exe()


Please, can someone help me with this silly string/command. The quotes between quotes keeps giving an error. Seriously, I have been hours try to get right the gooddamn quotes xd.

python3 -c "exec('import glob;things=glob.glob('/*');with open('test_output.txt', 'w') as f:  f.write('\n'.join(things))')"

SyntaxError: invalid syntax


Solution

  • you need to escape the inner quotes like so:

    python3 -c "exec('import glob;things=glob.glob(\'/*\');with open(\'test_output.txt\', \'w\') as f:  f.write(\'\n\'.join(things))')"
    

    I changed the escaped quotes, since I don't have the files, I can't properly test it, but it is now able to process the command and attempt to run it in python.