Python 用户如何输入文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15037803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does a user input a filename?
提问by Chris Searcy
I've written code for an assembler, but I am still new to python.
In my code I have the user input a file that will be converted into an assembly language. I think I've almost got it working, but I can't figure out where the user enters the file name.
I'm in (what I think is)IDLE, and then when I hit F5 it runs in the shell. I'm getting an error, but I'm pretty sure it's because no file name has been entered.
我已经为汇编程序编写了代码,但我还是 Python 的新手。
在我的代码中,我让用户输入一个将被转换为汇编语言的文件。我想我几乎可以正常工作了,但是我不知道用户在哪里输入文件名。
我处于(我认为是)空闲状态,然后当我按下 F5 时,它会在 shell 中运行。我收到一个错误,但我很确定这是因为没有输入文件名。
Where is the user supposed to input these kinds of things? Is this done from the python shell, or from the command line, do I need to turn it into an executable?
用户应该在哪里输入这些东西?这是从 python shell 完成的,还是从命令行完成的,我是否需要将其转换为可执行文件?
Can someone help clarify where the user is inputting all this information?
有人可以帮助澄清用户在哪里输入所有这些信息吗?
I'll put in a segment of code, although I don't think it's necessary to answer my questions, but maybe it'll give you a better idea of my issue.
我会放一段代码,虽然我认为没有必要回答我的问题,但也许它会让你更好地了解我的问题。
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
print 'need filename'
sys.exit(-1)
table = SymbolTable()
parser = Parser(sys.argv[1])
parser.advance()
line = 0
while parser.hasMoreCommands():
if parser.commandType() == 'L_COMMAND':
table.addEntry(parser.symbol(), line)
else:
line += 1
parser.advance()
code = Code()
parser = Parser(sys.argv[1])
parser.advance()
var_stack = 16
while parser.hasMoreCommands():
cmd_type = parser.commandType()
if cmd_type == 'A_COMMAND':
number = 32768
try:
addr = int(parser.symbol())
except:
if table.contains(parser.symbol()):
addr = table.getAddress(parser.symbol())
else:
table.addEntry(parser.symbol(), var_stack)
addr = var_stack
var_stack += 1
bin_number = bin(number | addr)[3:]
assembly = '0' + bin_number
print assembly
elif cmd_type == 'C_COMMAND':
assembly = '111'
assembly += code.comp(parser.comp())
assembly += code.dest(parser.dest())
assembly += code.jump(parser.jump())
print assembly
parser.advance()
The part to note is at the beginning lines 4-6 where it's checking the file name. So once I run my program I get 'need filename' printed to the screen and an error message that looks like this:
要注意的部分是在开始的第 4-6 行,它正在检查文件名。因此,一旦我运行我的程序,我就会在屏幕上打印出“需要文件名”和一条如下所示的错误消息:
Traceback (most recent call last):
File "C:\Python27\Assembler.py", line 98, in <module>
sys.exit(-1)
SystemExit: -1
So where can I input the filename to avoid this error?
那么我在哪里可以输入文件名来避免这个错误呢?
回答by Blender
The way you have it, Python expects the filename as an argument:
按照你的方式,Python 期望文件名作为参数:
python file.py your_file.asm
If you want to prompt for a filename, use raw_input()(or input()for Python 3):
如果要提示输入文件名,请使用raw_input()(或input()用于 Python 3):
filename = raw_input('Enter a filename: ') or 'default_file.asm'
回答by pradyunsg
sys.argvcontains command line arguments.
So, this script has to be run through command-line, for getting input, as said by blender, use raw_input(or input) for getting input from the user, if there are not enough command-line arguments.
sys.argv包含命令行参数。
因此,此脚本必须通过命令行运行,以获取输入,如 Blender 所说,如果没有足够的命令行参数,请使用raw_input(或input) 从用户获取输入。
Something like this:
像这样的东西:
if len(sys.argv) == 1:
print "You can also give filename as a command line argument"
filename = raw_input("Enter Filename: ")
else:
filename = sys.argv[1]
And change the line
并改变线
parser = Parser(sys.argv[1])
To
到
parser = Parser(filename)

