David Vasandani
David Vasandani

Reputation: 1930

python shell subprocess import call invalid syntax

I want to perform a shell command on each file in the directory. I keep getting errors.

Here is the code:

import os
from subprocess import call

for dirname, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        jpg = os.path.join(dirname, filename)
        call(["./curl_recognize.sh", jpg, jpg".txt", "-t txt"])

Here is the error:

    call(["./curl_recognize.sh", jpg, jpg".txt", "-t txt"])
                                              ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 2208

Answers (1)

javawizard
javawizard

Reputation: 1284

Add a + before ".txt". That'll fix it.

The + operator concatenates strings in Python. When both strings are constants, you can just put them next to each other ("foo" " bar" is the same as "foo bar"), but if one of them is a variable (or any other sort of expression), you have to use the + operator.

Unrelated problem in your code: you might also need to change "-t txt" to "-t", "txt", as the former will pass "-t txt" as a single argument to the program, and nearly all argument-parsing programs expect flags ("-t") and their values ("txt") as separate arguments.

Upvotes: 3

Related Questions