views:

69

answers:

2

Python is creating a folder in my directory every time I call this method. The method is in one of my Django applications that requires access to the server's local area.

def filepath(filename, foldername='', envar='MYAPPDIR'):

    if envar is not None and envar is os.environ:
        dirpath = os.environ[envar]
    else:
        dirpath = '~/myFolder/%s' % foldername    

    expanded = os.path.expanduser(dirpath)
    if not os.path.isdir(expanded):
        if os.path.lexists(expanded):
            raise IOError(errno.EEXIST, "Path is a file, nor a dir", expanded)
        os.makedirs(expanded)

    return os.path.join(expanded, filename)

I'd like to stop it from happening.

Please note: the user can specify if it's in another directory within the default. Therefore the default folder is myFolder, however if the user wants to use a folder called myOtherFolder within myFolder (therefore ~/myFolder/myOtherFolder/) then they can. This is the kind of functionality I'm trying to implement, hence my using folder='' if no argument is passed to the method(which I think is the problem).

A: 

I guess it's going into the second part of your initial if clause. Can you print out the values of envar and the keys in os.environ when you run this in your environment? That should give you your answer. Also, you realise that os.environ is the environment in which your server is running and is not dependent on the anything from the client, don't you?

Noufal Ibrahim
+3  A: 
def filepath(filename, foldername=None, envar='MYAPPDIR'):
  default = '~/myFolder'
  if foldername:
    default = os.path.join(default, foldername)
  dirpath = os.path.expanduser(os.environ.get(envar, default))

  try:
    os.makedirs(dirpath)
  except OSError as e:
    if e.errno != errno.EEXIST:
      raise
  return os.path.join(dirpath, filename)

Biggest change removing the "is" typo you had in the first if's condition (did you mean "in"?).

However, it appears you want envar to override "~/myFolder", not "~/myFolder/otherSpecifiedByUser", giving:

def filepath(filename, foldername=None, envar='MYAPPDIR'):
  dirpath = os.path.expanduser(os.environ.get(envar, "~/myFolder"))
  if foldername:
    dirpath = os.path.join(dirpath, foldername)

  try:
    os.makedirs(dirpath)
  except OSError as e:
    if e.errno != errno.EEXIST:
      raise
  return os.path.join(dirpath, filename)

Also, if you're storing config files, then you can trivially follow the XDG basedir spec, if you're really using something like "~/.myFolder":

def filepath(filename, foldername=None, envar='MYAPPDIR'):
  default = os.path.join(os.environ.get("XDG_CONFIG_HOME", "~/.config"), "myFolder")
  dirpath = os.path.expanduser(os.environ.get(envar, default))
  if foldername:
    dirpath = os.path.join(dirpath, foldername)

  try:
    os.makedirs(dirpath)
  except OSError as e:
    if e.errno != errno.EEXIST:
      raise
  return os.path.join(dirpath, filename)
Roger Pate
Unfortunately still doesn't work. Maybe I was a little unclear, I've just updated my original question.
day_trader
@theEconomist: I think you've made it more unclear. If you don't want to create a directory, then drop the whole makedirs part. (Delete everything in my code from "try" upto the "return".) You could check that the directory exists instead, or just return the result and let the higher level code handle that (since it sounds like it deals with similar issues anyway).
Roger Pate