Я Питон начинающий так, пожалуйста, помогите мне ...получение синтаксической ошибки вблизи неожиданного токена `; ' в питоне
#!/usr/bin/python -tt
import sys
import commands
def runCommands():
f = open("a.txt", 'r')
for line in f: # goes through a text file line by line
cmd = 'ls -l ' + line
print "printing cmd = " + cmd,
(status, output) = commands.getstatusoutput(cmd)
if status: ## Error case, print the command's output to stderr and exit
print "error"
sys.stderr.write(output)
sys.exit(1)
print output
f.close()
def main():
runCommands()
# Standard boilerplate at end of file to call main() function.
if __name__ == '__main__':
main()
Я запустить его следующим образом:
$python demo.py
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `; } 2>&1'
error
Запуск less $(which python)
говорит:
#!/bin/sh bin=$(cd $(/usr/bin/dirname "$0") && pwd) exec -a "$0" "$bin/python2.5" "[email protected]"
Если я удалить for loop
, тогда он отлично работает
$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
Я использую 'ls' только для того, чтобы показать проблему. На самом деле я хочу использовать некоторые внутренние сценарии оболочки, поэтому я должен запускать этот скрипт python таким образом.
Я только что запустил ваш код. Это нормально. – pyfunc
то что не так на моем конце :( –
Я запускаю эту команду - $ python demo.py –