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?
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: