【问题标题】:Difference between reading a filename from the user using argv and raw_input使用 argv 和 raw_input 从用户读取文件名的区别
【发布时间】:2013-06-03 08:28:49
【问题描述】:

Learning Python the Hard Way 中的练习 15 是关于开始使用两种方法在脚本中读取的文件:

  • 通过 argv 和
  • 通过 raw_input。

这是脚本:

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "I'll also ask you to type it again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

作者提出了一些问题。第五个是想办法:

why one method of getting the filename is better than the other.

我想知道使用其中一种是否有真正的优势。

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    使用命令行参数 (argv) 使其成为可能:交互式非交互式使用。要求启动参数永远不会干净。此外,在询问时,获得多行输入是非常不切实际的。

    Argv 为(简单)结构化输入提供了一个干净且众所周知的界面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2015-01-10
      • 2015-12-31
      • 1970-01-01
      • 2012-04-24
      • 2016-03-14
      相关资源
      最近更新 更多