[ ok I edit my question since I just got answers corresponding to the first lines of my previous post, and actually this was not the question... so if you post smtg please test before and read the whole thing before thinking I need to know how to test paths in python ;) ]
the whole script, which is detailed below : http://www.pasteall.org/38511/python
the script does not return the same on windows 8 / python33 and on winXP / python 322 and this is about dir exists tests on windows boxes.
say that Im using a windows 8 box with python 3.3
sys.version_info(major=3, minor=3, micro=0, releaselevel='final', serial=0)
I have this existing path on my hd:
D:\ImHere\meToo
a simple script that confirms me these directories exists (Im using a cmd admin prompt to execute the script) :
import os
from os import path
import sys
print(sys.version_info)
def there(p) :
print('%s %s'%(p,'exists' if os.path.isdir(p) else 'does not exist'))
# got True, ok
print('\nusuals')
path = "D:\ImHere\meToo"
there(path)
# got False, ok
path += "\ImNot"
there(path)
# a bit more academic, tests ok
elms = path.split('\\')
jpath = ''
for elm in elms :
jpath = os.path.join(jpath,elm)
there(jpath)
# ... ok
rpath = elms[0] + os.sep + elms[1] + os.sep + elms[2] + os.sep + elms[3]
there(rpath)
this returns in the console :
usuals
D:\ImHere\meToo exists
D:\ImHere\meToo\ImNot does not exist
D: exists
D:ImHere exists
D:ImHere\meToo exists
D:ImHere\meToo\ImNot does not exist
D:\ImHere\meToo\ImNot does not exist
but compare this :
# now, this 'flat loop' is ok too
print('\nflat loop')
wpath = elms[0]
wpath += os.sep + elms[1]
wpath += os.sep + elms[2]
wpath += os.sep + elms[3]
there(wpath)
got :
flat loop
D:\ImHere\meToo\ImNot does not exist
with this :
# but if one calls isdir() in the process
# something is altered, and this ALWAYS returns dir exists
print('\nfails ?')
wpath = elms[0]
wpath += os.sep + elms[1]
there(wpath)
wpath += os.sep + elms[2]
there(wpath)
wpath += os.sep + elms[3]
there(wpath)
win8 / python 33: KO
fails ?
D:\ImHere exists
D:\ImHere\meToo exists
D:\ImHere\meToo\ImNot exists
winXP / python 322: OK
fails ?
D:\ImHere exists
D:\ImHere\meToo exists
D:\ImHere\meToo\ImNot does not exist
it seems the call to isdir() break something in the string ?
FIRST POST
( sorry if its a duplicate, I guess this String concatenation in Python could help about the case, but as I get mad tonight I need to share this.)
I wrote this little loop in a module to create recursive directories when they don't exist. this works fine on xp, python 3.2 :
def mkdir(path) :
path = clean(path) # this one generates unix like path
parts = path.split('/')
tpath = parts[0]
for pi in range(1,len(parts)) :
tpath += '/%s'%parts[pi]
if os.path.isdir(tpath) == False :
os.mkdir(tpath)
but when using windows8 + python 33, the directory test always return true.. ? after hours digging and experimenting about quotes and slashes stuffs.. I found the issue was this line:
tpath += '/%s'%parts[pi]
and
tpath = '%s/%s'%(tpath,parts[pi])
solved the case.
what is very odd for me, and by odd I mean absolutely psychedelic, is that the generated string whatever the code looks like to be the same :
print(os.path.isdir(tpath),tpath==path)
gives me 'True True' for the last directory in the first case : generated and input string are the same (and types are string), but the last directory does not exist..
and after the edition it returns a 'False True' as it should.
Im very frightened. the world looks like very odd since a couple of hours. what append with string += concatenation type is ok, bool() tests are ok... ? please save me from madness.. thanks a lot.