1

I am writing a Python script to create directories for a given term and course. I would like to make use of the Python modules os, sys, and getopt (with both short and long form options) so that the running script would look like:

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

The code that I have write now looks like this:

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

After this, I have a function that takes in the term and course an uses os.mkdir and such:

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)

For some reason, the folder that gets made only has a name that represents the term rather than both the term and the course. I feel like this might have something to do with my use of getopt, but I'm not certain. Any advice?

xbello
  • 7,223
  • 3
  • 28
  • 41
  • 1
    I think getopt is deprecated in python2.7 http://stackoverflow.com/a/3217687/2530083 use `argparse` instead – rtrwalker Oct 14 '13 at 01:52
  • As a side comment, do you realise that `path = os.path.join(whatever)` and `os.makedirs(path)` lines are repeated? Refactor it with `for` looping through a list of dirs `["assignments", "examples", "exams", "lecture_notes", "submissions"]`. – xbello Jan 21 '14 at 07:48
  • @rtrwalker `optparse` is deprecated. `getopt` is kept for C users. – xbello Jan 21 '14 at 08:00

3 Answers3

1

os.path.join is a clever function. Just pass as many folders as you need:

>>> import os
>>> os.path.join("first", "second", "third")
'first/second/third'
xbello
  • 7,223
  • 3
  • 28
  • 41
0

I'm actually in your class; at least I'm almost positive I am. Was running into this exact problem and was having trouble getting that sub-directory. A google search landed me here! Well, after a few more googles and LOTS of troubleshooting, found the answer to our problem!

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

That should clean it up for you and give your your new directory and sub-directories! Good luck with the rest of the assignment.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
Kelly
  • 1
0

When you write term + course, Python concatenates the strings in term and course directly, before os.path.join() even sees them. That is, if, say, term == "fall2013" and course == "cs311-400", then term + course == "fall2013cs311-400" with nothing in between.

One way around that would be to insert an explicit slash between the term and the course, as in term + "/" + course. However, since you've presumably been instructed to use os.path.join() (which is a good idea, anyway), you can just pass all the path components you want to join to it as separate arguments and let it take care of joining them for you:

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

Also, a few tips for your assignment, and for good Python coding in general:

  • While the getopt module is not actually deprecated like rtrwalker claims in the comments, you're probably better off using argparse unless you have to use getopt for some reason (like, say, the assignment tells you to).

  • Your code looks very repetitive. Repetitive code is a "smell" that should suggest the need for a loop, perhaps like this:

    dirs = ("assignments", "examples", "exams", "lecture_notes", "submissions")
    for folder in dirs:
        path = os.path.join(term, course, folder)
        os.makedirs(path)
    
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153