Python:检查目录是否为别名 [英] Python: Check if a directory is an alias
问题描述
python是否具有简单的功能来检查目录是否为实际目录,或者仅仅是其他目录的别名?我试图列出目录中的所有文件/文件夹,但是由于这些别名文件夹,我迷失了看起来像这样的东西:
Does python have a simple function for checking if a directory is an actual directory or if it's just an alias to another directory? I'm trying to list all files/folders in a directory but because of these alias folders, I'm getting a lost of stuff that looks like this:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/捆绑/首页/捆绑/首页/捆绑/首页/捆绑
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle/Home/bundle
我知道我可以编写一个函数来比较路径并退出(如果看起来好像我正在绕圈时退出),但是有一个简单的函数确实可以执行我不知道的事情吗?
例如.
os.isAlias( …pathname… )
I know I can write a function that will compare paths and quit if it seems like I'm going in circles, but is there a simple function that does exactly that that I'm not aware of?
E.g.
os.isAlias( …pathname… )
谢谢!
推荐答案
这是os.path.realpath
的一个版本,可在Mac别名以及Python 2下的符号链接上使用:
Here's a version of os.path.realpath
that works on Mac aliases as well as on symbolic links under Python 2:
from Carbon import File
def osx_realpath (path):
return File.FSResolveAliasFile(path, True)[0].as_pathname()
如果在递归进入每个目录之前在每个目录上调用osx_realpath,则应避免重复.另外,您可以定义类似
If you call osx_realpath on each directory before you recurse into it you should avoid duplication. Alternatively you could define something like
def is_osx_realpath (path):
return path == osx_realpath(path)
在这里,您必须担心假阴性.如果您过滤is_osx_realpath且您以其开头的路径是别名,则程序将停止运行,而不会查看任何内容.
Here you have to worry a little about false negatives, however. If you filter for is_osx_realpath and the path you start with is an alias, your program will stop without looking at anything.
到目前为止,我还不知道在Python 3下执行此操作的方法.我有一个问题 subprocess.call 做得更好href ="https://stackoverflow.com/questions/1175094/os-x-terminal-command-to-resolve-path-of-an-alias/17570232#17570232">调用在命令行中执行检查的操作.
So far I don't know of a way to do this under Python 3. I have a question here where I'm hoping for an answer. Right now I can't do better than using subprocess.call to invoke something that does the check on the command line.
我应该补充的是,Carbon.File不仅在Python 3中不可用,而且已被弃用,因此最好在Python 2中避免使用-但是,这是我所知道的Python 2最实用的解决方案礼物.
I should add that not only is Carbon.File not available in Python 3, but it is deprecated and so is best avoided in Python 2 as well--however it's the most pragmatic solution I know of for Python 2 at present.
这是一种检查文件是否是我认为对Python 3友好的别名的方法.但是,我没有代码来解析别名.我相信您需要安装PyObjC.
EDIT 2: here is a way to check if a file is an alias that I believe to be Python 3-friendly. However, I don't have code to resolve the alias. I believe you need PyObjC installed.
from AppKit import NSWorkspace
def is_alias (path):
uti, err = NSWorkspace.sharedWorkspace().typeOfFile_error_(
os.path.realpath(path), None)
if err:
raise Exception(unicode(err))
else:
return "com.apple.alias-file" == uti
(源)
这篇关于Python:检查目录是否为别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!