简体   繁体   English

Python模块代码说路径不存在,但可读且可设定

[英]Python module code says path doesn't exist but is readable and statable

I'm running into a really bizarre problem in a python module and I'm not sure whether or not it's due to some python weirdness or some brokenness in windows or the filesystem being in a weird state. 我在python模块中遇到了一个非常奇怪的问题,我不确定这是否是由于python怪异或Windows中的某些损坏或文件系统处于怪异状态引起的。

Here's the problem: a module I'm using creates some output that goes into a specified directory, and it creates that directory if it doesn't exist. 这是问题所在:我正在使用的模块会创建一些输入到指定目录的输出,如果该目录不存在,则会创建该目录。 This works fine if you run it once, running it a second time the module thinks that the directory doesn't exist, tries to create it, then bombs out with a "Cannot create a file when that file already exists" error. 如果您运行一次,然后再次运行它,则模块认为该目录不存在,尝试创建该目录,然后以“当该文件已存在时无法创建文件”的错误炸弹,这将很好地工作。

So, I went digging. 所以,我去挖了。 In my main script I can run os.path.exists on the directory and it says it exists just fine. 在我的主脚本中,我可以在目录上运行os.path.exists,它说它存在就可以了。 In the module the same exact code returns false however. 在模块中,相同的确切代码返回false。 Also in the module if I do an os.access( path , os.R_OK) it returns true, indicating the path is readable, and if I do an os.stat( path ) it returns file info, indicating the path exists. 同样在模块中,如果我执行os.access( path ,os.R_OK),则返回true,表示该路径可读;如果执行os.stat( path ),则返回文件信息,表明该路径存在。

If I run the same code manually in a python shell it all works fine, says the path exists, etc. 如果我在python shell中手动运行相同的代码,则一切正常,表示路径存在,等等。

Here's some code and the outputs: 这是一些代码和输出:

In my main script: 在我的主脚本中:

print('Path {} exists: {} - readable: {} - stat: {}', 
    filename, 
    os.path.exists(filename), 
    os.access(filename, os.R_OK), 
    os.stat(filename))

Output: 输出:

File testoutput exists: True - readable: True - stat: nt.stat_result(st_mode=16895, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1508458382L, st_mtime=1508458382L, st_ctime=1508458382L)

Inside the module, the same code: 在模块内部,相同的代码:

File testoutput exists: False - readable: True - stat: nt.stat_result(st_mode=16895, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1508458382L, st_mtime=1508458382L, st_ctime=1508458382L)

What the heck is going on here? 这里到底发生了什么? Is the module code running in some sort of different context with different permissions? 模块代码是否在具有不同权限的某种不同上下文中运行? Is my filesystem broken in some weird way? 我的文件系统是否以某种奇怪的方式损坏了?

Also, I should mention that all of this is being done in an elevated cmd shell so that's not the problem. 另外,我应该提到所有这些都是在高架cmd外壳中完成的,所以这不是问题。


Solved: 解决了:

It turned out to be some intervening code that was monkey patching os.path.exists, the most helpful tool was using reflection and the "inspect" module to figure out where the code was actually coming from, specifically: 原来,这是一些猴子补丁os.path.exists的中间代码,最有用的工具是使用反射和“检查”模块来找出代码的实际来源,特别是:

    print('os.path: {} - {} - {} - {}'.format(os.__file__, os.path.__name__, os.path.__file__, os.path.exists.__module__))
    print(inspect.getsource(os.path.exists))

The __module__ info told me where the monkeypatching was happening. __module__信息告诉我monkeypatching在哪里发生。

This is just an educated guess. 这只是有根据的猜测。 os.path.exists will return False for a broken symlink, while os.access will report the file itself (the symlink) as being readable. 对于断开的符号链接, os.path.exists将返回False ,而os.access将报告文件本身(符号链接)为可读。 Is it possible your file is a symlink? 您的文件是否可能是符号链接?

Another difference is that os.access will use the real (not effective) uid/gid to test readability while os.path.exists uses the effective uid/gid. 另一个区别是os.access将使用实际的(无效的)uid / gid来测试可读性,而os.path.exists使用的是有效的uid / gid。 Is it possible there is a change of user ID between the modules? 模块之间的用户ID是否可能更改?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM