[Windows 7, Python 2.6] Can't write to a directory made w/ os.makedirs

Tim Golden mail at timgolden.me.uk
Sun Jan 1 04:37:52 EST 2012

On 31/12/2011 22:13, OlyDLG wrote:
 > Hi!  I'm working on a script utilizing os.makedirs to make directories
 > to which I'm then trying to write files created by exe's spawned w/
 > subprocess.call; I'm developing in Stani's Python Editor, debugging
 > using Winpdb.  I've gotten to the point where
 > subprocess.Popen._execute_child is raising a WindowsError(5,'Access is
 > denied') exception.  I've tried: setting the mode in the makedirs
 > call; using os.chmod(<dir>, stat.S_IWRITE); and even setting a
 > breakpoint before the subprocess.call and unsetting the read-only
 > attribute of the pertinent directory tree using the Windows directory
 > Properties dialog--which doesn't "take," if that's a clue--all to no
 > avail.

Whatever you're trying to do, resetting the read-only bit
on the Windows directory is not the answer. It has, bizarrely,
nothing to do with the read/write-ness of a directory; rather,
it reflects the system-ness of a folder.

There's no general answer I can see to give, without
knowing what the security is on your folders or what
other thing might impede your processes from writing
files in them. Is it definite that the ERROR_ACCESS_DENIED
is in fact the result of attempting to write a file into
one of the newly-created directories?

Can you do something like this (adapt for your own environment):

import os, sys
import subprocess

os.makedirs ("c:/temp/a/b/c")

with open ("c:/temp/a/b/c/test1.txt", "w"):

subprocess.call ([
   "open ('c:/temp/a/b/c/test2.txt', 'w').close ()"


ie can the Python process creating the directories,
and a subprocess called from it create a simple file?

Depending on where you are in the filesystem, it may indeed
be necessary to be running as administrator. But don't try
to crack every security nut with an elevated sledgehammer.


More information about the Python-list mailing list