-3

I don't know how to fix the syntax

import os, sys, subprocess

subprocess.call("grep -o 'tag[^"]*' test > update", shell=True)
    subprocess.call("grep -o 'tag[^"]*' test > update", shell=True)
                                    ^
SyntaxError: invalid syntax

I tried using os.system as well but i get the same error

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

3

Python strings are delimited by quotes, in your case the double quote that starts "grep -o .... But there's also a double quote inside 'tag[^"]*:

subprocess.call("grep -o 'tag[^"]*' test > update", shell=True)
#                             ~^~

So Python sees this as

                "grep -o 'tag[^"
subprocess.call(                ]*' test > update", shell=True)
                                

Which is invalid syntax. You need to escape the double quotes by putting \ before it:

subprocess.call("grep -o 'tag[^\"]*' test > update", shell=True)
#                              ^ Added this

or use a different pair of quotes, like triple ':

subprocess.call('''grep -o 'tag[^\"]*' test > update''', shell=True)
#               ^^^              changed these      ^^^
BoppreH
  • 8,014
  • 4
  • 34
  • 71