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

  • SEARCH
  • Home
  • 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 9201273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:57:58+00:00 2026-06-17T22:57:58+00:00

I am writing a program in Python 2.7 that retrieves remote files and dumps

  • 0

I am writing a program in Python 2.7 that retrieves remote files and dumps them in a directory that can be specified by the user. Currently, in order to verify that the program can in fact write to that directory, I do the following (assuming that dumpdir is a string containing the name of the directory to check):

try:
    os.mkdir(dumpdir+'/.mwcrawler')
    os.rmdir(dumpdir+'/.mwcrawler')
except:
    logging.error('Could not open %s for writing, using default', dumpdir)

But this feels even more hackish than my usual code. What’s the correct way to go about this? Maybe some sort of assertion on privileges?

  • 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-06-17T22:58:00+00:00Added an answer on June 17, 2026 at 10:58 pm

    In general, it’s better to ask for forgiveness than permission—you have to handle errors in writing each file anyway, so why check in advance?

    But, when you have a user interface—even a command-line interface, where you may read a prefs file long before you get to any writing—it’s often much more convenient to the user to return errors as soon as possible. As long as that’s the only reason you’re doing this check, there’s nothing wrong with it.

    However, there are many little ways you could improve the way you do the check.

    First, you should almost never just use except: without specifying anything. Besides the fact that this catches different things in different versions of Python (and therefore also confuses human readers used to other versions), it means you have no way of distinguishing between not writable, a bad Unicode character, or even a typo in the code.

    Plus, your error message says “not readable” if it can’t write, which is pretty odd.

    Second, unless you’re sure nobody will ever have a file named .mwcrawler (e.g., because you refuse to transfer files starting with '.' or something), using any fixed name is just asking for trouble. A better solution is to use, e.g., tempfile.mkdtemp.

    Also, you should avoid using string manipulation for paths if you want to be portable. That’s what os.path (and higher-level utilities) are for—so you don’t have to learn or think about Windows, zOS, etc.

    Putting it all together:

    try:
        d = tempfile.mkdtemp(prefix='.mwcrawler', dir=dumpdir)
    except Exception as e:
        logging.error('Could not open %s for reading (%s), using default', dumpdir, e)
    else:
        os.rmdir(d)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a basic program in Python that prompts the user to enter
I'm writing a python program that deals with a fair amount of strings/files. My
I'm writing a Python program that, if the user changes settings while running it,
I am writing a Python program that requires a user to input their gmail
I am writing a program in python that will take user birthday input and
I have a program in python 2.7 that is writing certain files and directories
I am writing a Python program that can login Gmail. The purpose of this
I'm writing a personal wiki-style program in Python that stores text files in a
I'm writing a python program that uses scons to build an .exe and then
I am writing a python program that lauches a subprocess (using Popen). I am

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.