getting syntax error near unexpected token `;' in python -
i python novice please me out...
#!/usr/bin/python -tt import sys import commands def runcommands(): f = open("a.txt", 'r') line in f: # goes through text file line line cmd = 'ls -l ' + line print "printing cmd = " + cmd, (status, output) = commands.getstatusoutput(cmd) if status: ## error case, print command's output stderr , exit print "error" sys.stderr.write(output) sys.exit(1) print output f.close() def main(): runcommands() # standard boilerplate @ end of file call main() function. if __name__ == '__main__': main() i run follows:
$python demo.py sh: -c: line 1: syntax error near unexpected token `;' sh: -c: line 1: `; } 2>&1' error running less $(which python) says:
#!/bin/sh bin=$(cd $(/usr/bin/dirname "$0") && pwd) exec -a "$0" "$bin/python2.5" "$@" if remove for loop works fine
$cat a.txt dummyfile $ls -l dummyfile -rw-r--r-- 1 blah blah ................... $python demo.py printing cmd = ls -l dummyfile sh: -c: line 1: syntax error near unexpected token `;' sh: -c: line 1: `; } 2>&1' error i using 'ls' showing problem. wanna use internal shell scripts have run python script in way only.
the problem caused line:
cmd = 'ls -l ' + line it should modified to:
cmd = 'ls -l ' + line.strip() when read line text file, read trailing \n. need strip works. getstatusoutput() doesn't trailing newline. see interactive test (which how verified it):
in [7]: s, o = commands.getstatusoutput('ls -l dummyfile') in [8]: s, o = commands.getstatusoutput('ls -l dummyfile\n') sh: syntax error: ";" unexpected
Comments
Post a Comment