0

I'm trying to automate our personal folder creation using a IDM system that runs on Python. All user shares will be created on \\\server\personal\%userID%. The \\\server\personal and %userID% are being passed as default_settings.home_directory and profileid. The code I'm trying as this :

share = self.config.getID('default_settings.home_directory') +  profileid
self.log.info('Share = [{0}]'.format(share))
os.makedirs(share)

In the log I have

Share = [\\server\personal\xr2829] 

but it errors out with

WindowsError: [Error 123] The filename, directory name, or volume label syntax is  incorrect: '\\\\'
ren
  • 313
  • 1
  • 3
  • 6
  • The '...\xr2829' in the log looks suspicious like an invalid string literal hexadecimal escape sequence which should be the form '\xhh' where the 'h's must be a hexadecimal digits [0..9][a..f]. Where are the backslashes coming from? – martineau Aug 27 '12 at 21:23
  • The 'profileid' is a randomly generated user ID. That is created at the beginning and then I just use the variable. – ren Aug 27 '12 at 21:25
  • The backslashes have me confused also. IDM means Identity Management system. – ren Aug 27 '12 at 21:26
  • Did you copy and paste the actual code into the question? The error message indicates it's not receiving the same string that the log recorded. – Mark Ransom Aug 27 '12 at 21:39
  • Backslash characters themselves have to be escaped (doubled) in Python string literals otherwise they're interpreted as the beginning of an escape sequence. Check the code that's generating the random profile id because that's where the backslash in '\xr2829' appears to be originating. – martineau Aug 27 '12 at 21:40
  • Yes I copy and pasted the code directly into the question. – ren Aug 27 '12 at 21:44
  • Could you add a `print repr(profileid)` or `self.log.info(repr(profileid))` to your code and see if it is `'\\xr2829'` as it should be? – martineau Aug 27 '12 at 21:47
  • Can you do a `os.makedirs('\\\\corpfilesrv\\personal$\\xr2829')` in the Python shell without an error (after an `import os`)? – martineau Aug 29 '12 at 02:14
  • Receive the same error when I gave it the path instead of passing the variable. – ren Aug 29 '12 at 17:34
  • Sounds like a bug in Python's `os.makdirs()` or the underlying file system. – martineau Aug 30 '12 at 01:27

2 Answers2

0

Try this alternative:

os.makedirs(os.path.join(self.config.getID('default_settings.home_directory'), profileid))

Should take care of slash/backslash confusion

schacki
  • 9,401
  • 5
  • 29
  • 32
  • `os.path` is the name of a submodule of the `os` module, not the name of a function in it. This doesn't look like a slash/backslash issue to me anyway. – martineau Aug 27 '12 at 21:52
  • good catch, I edited my solutin, just in case that it is a slash issue :-) – schacki Aug 27 '12 at 22:23
  • I gave this code a try and received the same error as before. – ren Aug 28 '12 at 17:04
0

I'm going to guess that there's a null character after the // in your configuration string. Python won't care, but the C function backing os.makedirs will stop on a null. Try logging repr(share) which will give a more detailed representation of the string.

Edit: Looking at the os.makedirs function more closely suggests another possibility. The path is being broken down into components so that each one can be checked or created as needed. The error message implies that the first two slashes (\\\\ when displayed using repr) have been broken out as the first directory element. The documentation says that UNC paths are supported as of version 2.3, but perhaps you're using a very old Python version or there's still a bug.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • When I use `repr(share)` I don't see anything in the log. Am I missing something?
    Update using `self.log.info(repr(share))` I was able to see this in the log `INFO - '\\\\corpfilesrv\\personal$\\xr2829'`
    – ren Aug 28 '12 at 17:07
  • @user1628698 well that ruins my theory. There's no null in that string, it would appear as a `\x00`. – Mark Ransom Aug 28 '12 at 17:51
  • I'm on version 2.7 but it could still be a bug. – ren Aug 28 '12 at 19:08
  • @user1628698: Interesting how there wasn't a `$` in the log after `personal` until you used `repr()` on it -- maybe that's a clue. – martineau Aug 29 '12 at 02:10
  • The issue is getting stranger. The code works when creating the folder on a physical server share. Our personal folders reside on a CIFS and I receive the Error 123 only on the CIFS. Is there something differnet with a CIFS?? – ren Aug 29 '12 at 17:32
  • @ren: Yes, going through CIFS is different. You could probably write your own `makedirs()` and figure out exactly what part of the process is failing...and maybe devise a workaround, depending on what you find. – martineau Aug 30 '12 at 01:24