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).