本文介绍了从文件执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 感谢您的时间。 我们有非常大的python命令文件(或多或少,500000 命令每个文件)。 可以通过命令执行它们,就像在 命令之后键入一个命令一样另一个在交互式会话中? (更好地使用命令标志而不是像1: input())这样的小脚本/> 非常感谢。Hello,Thanks for your time.We have very big files with python commands (more or less, 500000commands each file).It is possible to execute them command by command, like if thecommands was typed one after the other in a interactive session?( Better using command flags than with an small script like "while 1:input()" )Thanks a lot.推荐答案 看一下标准库中的`code`模块: 在[31]中:代码? 类型:模块 基类:<类型''模块''> 字符串形式:< module''code''来自''/usr/lib/python2.4/code.pyc''> 命名空间:互动 文件:/usr/lib/python2.4/code .py 文档字符串: 实用程序需要模拟Python的交互式解释器。 Ciao, Marc''BlackJack''RintschTake a look at the `code` module in the standard library:In [31]: code?Type: moduleBase Class: <type ''module''>String Form: <module ''code'' from ''/usr/lib/python2.4/code.pyc''>Namespace: InteractiveFile: /usr/lib/python2.4/code.pyDocstring:Utilities needed to emulate Python''s interactive interpreter.Ciao,Marc ''BlackJack'' Rintsch 这些是大项目。据推测其他程序正在编写它们?Those are BIG programs. Presumably other programs are writing them? 您需要查找交互式Python调试器pdb。这是 ,能够进行单步操作,并支持断点。You need to look for "pdb", the interactive Python debugger. This iscapable of single-step operations, and supports breakpoints. 你几乎必须运行pdb然后通过 调用pdb方法调用代码中的函数作为参数触发你的代码,如果我 我正确记得如何它有效。 问候 史蒂夫 - Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC / Ltd http:// www。 holdenweb.com Skype:holdenweb http: //del.icio.us/steve.holden ------------------ Asciimercial -------- ------------- 上网:博客,镜头并标记你的名声!! holdenweb.blogspot.com squidoo.com/pythonology 标记物品:del.icio.us/steve.holden/python 所有这些服务目前都提供免费注册! --------------感谢您的阅读---------- ------You are pretty much going to have to run pdb then trigger your code bycalling a pdb method with a function in your code as an argument, if Iam remembering correctly how it works.regardsSteve--Steve Holden +1 571 484 6266 +1 800 494 3119Holden Web LLC/Ltd http://www.holdenweb.comSkype: holdenweb http://del.icio.us/steve.holden------------------ Asciimercial ---------------------Get on the web: Blog, lens and tag your way to fame!!holdenweb.blogspot.com squidoo.com/pythonologytagged items: del.icio.us/steve.holden/pythonAll these services currently offer free registration!-------------- Thank You for Reading ---------------- inp = open(cmd_file) for in in line: exec line 可能有所帮助。你不会有像 那样的感觉,就好像命令是在交互式会话中一个接一个地输入 一样,但也许这会有所帮助。 警告:上面的代码没有任何错误检查。 您可能还会遇到安全问题,例如 以上假设你信任你的意见。 HTH。 YMMV。 马丁inp = open(cmd_file)for line in inp:exec linemight help. You don''t get quite the same feeling as"like if the commands was typed one after the otherin a interactive session", but perhaps this helps.Warning: the code above is without any error checks.You might also run into security problems, the exampleabove assumes you trust your input.HTH. YMMV.Martin 这篇关于从文件执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 04:09