Windows 8 使用python 33/isdir()的字符串连接总是返回True吗?(3小时头部碰撞)

Windows 8 使用python 33/isdir()的字符串连接总是返回True吗?(3小时头部碰撞),windows-8,python-3.x,Windows 8,Python 3.x,[好的,我编辑了我的问题,因为我刚刚得到了与我上一篇文章的第一行相对应的答案,实际上这不是问题……因此,如果你发布smtg,请在发布之前进行测试,并在考虑我需要知道如何在python中测试路径之前阅读全部内容;)] 整个脚本,详细内容如下: 在windows 8/python33和winXP/python322上,脚本不会返回相同的结果 这是关于windows机器上的dir测试 假设我在使用Windows8和Python3.3 sys.version_info(major=3, minor=3

[好的,我编辑了我的问题,因为我刚刚得到了与我上一篇文章的第一行相对应的答案,实际上这不是问题……因此,如果你发布smtg,请在发布之前进行测试,并在考虑我需要知道如何在python中测试路径之前阅读全部内容;)]

整个脚本,详细内容如下:

在windows 8/python33和winXP/python322上,脚本不会返回相同的结果 这是关于windows机器上的dir测试

假设我在使用Windows8和Python3.3

sys.version_info(major=3, minor=3, micro=0, releaselevel='final', serial=0)
我的硬盘上有一条现有路径:

D:\ImHere\meToo
一个简单的脚本可以确认这些目录的存在(我使用cmd admin提示符来执行该脚本):

这将在控制台中返回:

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
但相比之下:

# 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)
得到:

为此:

# 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/python322:OK

fails ?
D:\ImHere exists
D:\ImHere\meToo exists
D:\ImHere\meToo\ImNot does not exist
对isdir()的调用似乎破坏了字符串中的某些内容

第一个职位

(对不起,如果是重复的,我猜是这个 我可以帮忙处理这个案子,但今晚我很生气,我需要和大家分享一下。)

我在一个模块中编写了这个小循环,以便在递归目录不存在时创建它们。这在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)
但是当使用windows8+python 33时,目录测试总是返回true? 经过几个小时的挖掘和实验,引用和删减了一些东西。。我发现问题在于这一行:

tpath += '/%s'%parts[pi]

破案了

对我来说非常奇怪的是,我所说的“奇”是指绝对迷幻的,无论代码看起来是什么,生成的字符串都是相同的:

print(os.path.isdir(tpath),tpath==path)
在第一种情况下,为最后一个目录提供“True”: 生成的字符串和输入字符串相同(类型为字符串),但最后一个目录不存在

在编辑之后,它会返回一个应该返回的“False-True”

我非常害怕。几个小时后,这个世界看起来很奇怪。使用字符串+=连接类型的追加是ok,bool()测试是ok?请把我从疯狂中解救出来。。非常感谢。

总之: 正如Mark Dickinson提到的,python 3.3中的os.path.isdir()存在一个与Unicode格式()相关的bug。 它看起来在下一个版本和alpha中得到了修复

同时,为了测试给定路径中是否存在文件夹名称,我在测试循环中使用os.path.join()而不是“path+=”字符串连接,得到了一致的结果,无论操作系统和python>=3.2版本是什么:

path = "D:\ImHere\meToo\Imnot"

def there(p) :
    print('%s %s'%(p,'exists' if os.path.isdir(p) else 'does not exist'))

elms = path.split('\\')
jpath = ''
for elm in elms :
    jpath = os.path.join(jpath,elm)
    there(jpath)
因此,在Python3.3中,不要将+连接与isdir()一起使用,而应使用join()连接


[等待Mark批准:)

您应该看看。为什么不使用?除此之外(因为它更普遍有用),为什么不使用
os.path.join
组合路径?(当然,这并没有否定这个问题。)还有,为什么不使用,因为路径分隔符在不同的平台上是不同的?最后,我输入得太慢了:-D
tpath = '%s/%s'%(tpath,parts[pi])
print(os.path.isdir(tpath),tpath==path)
path = "D:\ImHere\meToo\Imnot"

def there(p) :
    print('%s %s'%(p,'exists' if os.path.isdir(p) else 'does not exist'))

elms = path.split('\\')
jpath = ''
for elm in elms :
    jpath = os.path.join(jpath,elm)
    there(jpath)