Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:43:51+00:00 2026-05-13T15:43:51+00:00

Python is creating a folder in my directory every time I call this method.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:43:51+00:00Added an answer on May 13, 2026 at 3:43 pm
    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python uses the reference count method to handle object life time. So an object
I have got a python script which is creating an ODBC connection. The ODBC
I'm creating a networked server for a boggle-clone I wrote in python, which accepts
If you are creating a 1d array in Python, is there any benefit to
I am creating a GUI frontend for the Eve Online API in Python. I
What is the best way of creating an alphabetically sorted list in Python?
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python works on multiple platforms and can be used for desktop and web applications,
Python's convention is that variables are created by first assignment, and trying to read
I'm using a open-source Python library in my project. This library logs a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.