简体   繁体   English

从控制台运行python脚本

[英]Running python script from console

Totally new to python, and i want to do the following: 完全是python的新手,我想执行以下操作:

I have this code: 我有以下代码:

def assem(myFile):
print "Hello ,World!" 
import myParser
from myParser import Parser
import code
import symboleTable
from symboleTable import SymboleTable


newFile = "Prog.hack"
output = open(newFile, 'w')
input = open(myFile, 'r')


prsr=Parser(input)
while prsr.hasMoreCommands():
    str = "BLANK"
    if(parser.commandType() == Parser.C_COMMAND):
        str="111"+code.comp(prsr.comp())+code.dest(prsr.dest())+code.jump(prsr.jump())+"\n"

    output.write(str)
    prsr.advance()

checked the indentation, its ok,its a bit messy over here. 检查了压痕,没关系,这里有点乱。

this program needs to run from console and receive a file named Add.asm 该程序需要从控制台运行,并接收一个名为Add.asm的文件。

what is the console command to make it run? 要使它运行的控制台命令是什么?

tried : 尝试过的

  python assembler.py Add.asm

did not work. 不工作。

any idea? 任何想法?

这就是您要寻找的: http : //docs.python.org/library/optparse.html

optparse is indeed what you will need for more advanced cl options. 对于更高级的cl选项,确实需要optparse。 However, you could python assembler.py <filename> with a simple if __name__ == "__main__" block. 但是,您可以使用简单的if __name__ == "__main__"块来python assembler.py <filename> In lieu of argparse or optparse , you can use sys.argv[1] for a single simple argument to the script. 可以使用sys.argv[1]代替脚本的argparseoptparse

def assem(myFile):
    print "Hello ,World!" 
    import myParser
    from myParser import Parser
    import code
    import symboleTable
    from symboleTable import SymboleTable

    newFile = "Prog.hack"
    output = open(newFile, 'w')
    input = open(myFile, 'r')
    prsr = Parser(input)

    while prsr.hasMoreCommands():
        str = "BLANK"
        if(parser.commandType() == Parser.C_COMMAND):
            str= "111" + code.comp(prsr.comp()
                ) + code.dest(prsr.dest()) + code.jump(prsr.jump()
                ) + "\n"

        output.write(str)
        prsr.advance()

if __name__ == "__main__":
    import sys
    assem(sys.argv[1])

You'll also want to google python string formatting and find links like http://docs.python.org/library/stdtypes.html#string-formatting 您还需要对python string formatting进行google搜索,并找到类似http://docs.python.org/library/stdtypes.html#string-formatting的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从控制台运行 python 脚本时出现 SSLError - SSLError while running python script from console 键盘快捷键破坏了从脚本运行交互式Python控制台 - Keyboard shortcuts broken running interactive Python Console from a script 从 shell 控制台手动运行 python3 脚本和命令 - Manually running python3 script and commands from shell console 从 `console_scripts` 运行脚本但通过 Python 控制台导入时不会出现 `ModuleNotFoundError` - `ModuleNotFoundError` when running script from `console_scripts` but not when importing via Python console 运行 Python 脚本让错误出现在控制台中 - Running Python script lets error appear in console 使用cron运行Python软件包控制台脚本 - Running Python package console script with cron 从控制台运行python脚本时py vs python有什么区别 - When running a python script from the console what is the difference between py vs python 从另一个python文件运行python脚本时如何显示控制台输出 - How to show console output when running a python script from another python file 如何从隐藏控制台 python 脚本运行 python 控制台脚本? - How run a python console script from a hiding console python script? 从ST编辑器运行时如何在控制台中显示python脚本的结果? - How to show result of python script in a console when running from ST editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM