os.makedirs: difference between nt4 and w2k

djw dwelch at nospam.vcd.hp.com
Thu Sep 26 12:29:04 EDT 2002


Jens Bloch Helmers wrote:
> Suppose unc_path is a UNC path refering to a non existing
> directory on another computer.
> os.makedirs( unc_path ) throws an exception if this path
> is on a windows 2000 machine but creates the directory if
> it is on a win-nt machine.
> 
> Is this a feature of win2k, or could I change the setup
> of that win2k computer to accept the creation of unc_path?
> Could this be done from Python?
> (I have administrator privileges on both computers)
> 
> Jens Helmers

I recently found that os.makedirs( <unc_path> ) does not work properly 
on win2k (haven't tried winnt). So, instead, I created a loop that walks 
the path one level at a time and calls win32file.CreateDirectory() for 
each directory level that os.path.isdir() returns false (or, you can 
simply catch the exception code rc==183 if the directory already exists, 
and continue).  I used split('\\') to generate the list of directories 
to create (element 2 ends up being the server name, element 3 the share 
name, and the slice [4:] is the remainder of the directories). Of 
course, this ends up being very Windows specific, but that doesn't sound 
like an issue for you.

So, _roughly_:

path = '\\\\server\\share\\dir1\\dir2\\'
p = path.split( '\\' )
trg = "".join( [ '\\\\', p[2], '\\', p[3] )  # \\server\share
subdirs = p[4:]  # [dir1, dir2, ...]

for subdir in subdirs:
	trg = os.path.join( trg, subdir )
	
	if not os.path.isdir( trg ):
		try:
			win32api.CreateDirectory( trg, None )
		except win32api.error, (rc, fnerr, msg ):
			if rc == 3 or rc == 53:
				print msg # File not found error
				# Do something intelligent here...
			else:
				print msg # Some other error

Hope this helps.

Don




More information about the Python-list mailing list