【问题标题】:Using getopt() to get values passed from the command line使用 getopt() 获取从命令行传递的值
【发布时间】:2013-10-21 12:49:46
【问题描述】:

我正在编写一个 Python 脚本来为给定的术语和课程创建目录。我想使用 Python 模块 os、sys 和 getopt(具有短格式和长格式选项),以便运行脚本如下所示:

>python directory.py –t fall2013 –c cs311-400 
>python directory.py –-term fall2013 –-class cs311-400

我现在写的代码是这样的:

import os
import sys
import getopt

term = ""
course = ""

options, args = getopt.getopt(sys.argv[1:], 't:c:', ['term=', 'course='])

for opt, arg in options:
    if opt in ('-t', '--term'):
        term = arg
    elif opt in ('-c', '--course'):
         course = arg

在此之后,我有一个函数,它接受术语和课程并使用 os.mkdir 等:

def make_folders(term, course):
    if not os.path.isdir(term + course):
        os.mkdir(term + course)
        path = os.path.join(term + course, "assignments")
        os.makedirs(path)

        path = os.path.join(term + course, "examples")
        os.makedirs(path)

        path = os.path.join(term + course, "exams")
        os.makedirs(path)

        path = os.path.join(term + course, "lecture_notes")
        os.makedirs(path)

        path = os.path.join(term + course, "submissions")
        os.makedirs(path)

make_folders(term, course)

由于某种原因,生成的文件夹只有一个代表学期的名称,而不是学期和课程的名称。我觉得这可能与我使用 getopt 有关,但我不确定。有什么建议吗?

【问题讨论】:

  • 我认为 getopt 在 python2.7 中已被弃用 stackoverflow.com/a/3217687/2530083 使用 argparse 代替
  • 作为旁注,您是否意识到path = os.path.join(whatever)os.makedirs(path) 行是重复的?使用for 循环通过目录列表["assignments", "examples", "exams", "lecture_notes", "submissions"] 重构它。
  • @rtrwalker optparse 已弃用。 getopt 为 C 用户保留。

标签: python unix command-line command-line-arguments getopt


【解决方案1】:

当您编写term + course 时,Python 会直接连接termcourse 中的字符串,甚至在os.path.join() 看到它们之前。也就是说,如果term == "fall2013"course == "cs311-400",那么term + course == "fall2013cs311-400" 之间没有任何内容。

解决此问题的一种方法是在术语和课程之间插入一个明确的斜线,如term + "/" + course。但是,由于您可能已被指示使用 os.path.join()(无论如何这是个好主意),您可以将要加入的路径组件作为单独的参数传递 all 并让它会帮你加入他们:

path = os.path.join(term, course, "exams")

此外,还有一些关于你的作业和一般良好 Python 编码的提示:

  • 虽然 getopt 模块实际上并没有像 cmets 中的 rtrwalker 声明那样被弃用,但您最好使用 argparse,除非您在某些情况下使用 getopt原因(比如说,作业告诉你这样做)。

  • 您的代码看起来非常重复。重复代码是"smell",应该表明需要循环,可能是这样的:

    dirs = ("assignments", "examples", "exams", "lecture_notes", "submissions")
    for folder in dirs:
        path = os.path.join(term, course, folder)
        os.makedirs(path)
    

【讨论】:

    【解决方案2】:

    os.path.join 是一个聪明的函数。只需传递所需数量的文件夹:

    >>> import os
    >>> os.path.join("first", "second", "third")
    'first/second/third'
    

    【讨论】:

      【解决方案3】:

      我实际上在你的班上;至少我几乎是肯定的。遇到了这个确切的问题,并且无法获取该子目录。谷歌搜索让我来到这里!好吧,经过几次谷歌和大量故障排除后,找到了我们问题的答案!

      os.makedirs(''+term+'/'+course+'')      
      path = os.path.join(''+term+'/'+course+'', "exams")
      os.makedirs(path)
      

      这应该为您清理它并提供您的新目录和子目录!祝你接下来的作业好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-22
        • 1970-01-01
        相关资源
        最近更新 更多