我是 Python 新手,所以请帮助我...
#!/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" "$@"
如果我删除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' 只是为了显示问题。实际上我想使用一些内部 shell 脚本,所以我只能以这种方式运行这个 python 脚本。