在OSX上用Python替换现在已弃用的Carbon.File.FSResolveAliasFile?

在OSX上用Python替换现在已弃用的Carbon.File.FSResolveAliasFile?,第1张

概述在 Python 2中,我可以使用以下代码来解析MacOS别名或符号链接: from Carbon import FileFile.FSResolveAliasFile(alias_fp, True)[0].as_pathname() 其中alias_fp是我很好奇的文件的路径,存储为字符串(source). 但是,the documentation cheerfully tells me th 在 Python 2中,我可以使用以下代码来解析MacOS别名或符号链接:

from Carbon import filefile.FSResolveAliasfile(alias_fp,True)[0].as_pathname()

其中alias_fp是我很好奇的文件的路径,存储为字符串(source).

但是,the documentation cheerfully tells me that the whole Carbon family of modules is deprecated.我应该使用什么呢?

编辑:我相信下面的代码是朝着PyObjC方法的正确方向迈出的一步.它不解析别名,但它似乎检测到它们.

from AppKit import NSWorkspacedef 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

(source)

不幸的是,我无法让@ Milliways的解决方案工作(对Cocoa一无所知),stuff I find elsewhere on the internet看起来要复杂得多(也许它处理各种边缘情况?).

解决方法 PyObjC桥允许您访问NSURL的书签处理,这是别名的现代(向后兼容)替代:

import os.pathfrom Foundation import *def target_of_alias(path):    url = NSURL.fileURLWithPath_(path)    bookmarkData,error = NSURL.bookmarkDataWithContentsOfURL_error_(url,None)    if bookmarkData is None:        return None    opts = NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithoutMounting    resolved,stale,error = NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsstale_error_(bookmarkData,opts,None,None)    return resolved.path()def resolve_links_and_aliases(path):    while True:        alias_target = target_of_alias(path)        if alias_target:            path = alias_target            continue        if os.path.islink(path):            path = os.path.realpath(path)            continue        return path
总结

以上是内存溢出为你收集整理的在OSX上用Python替换现在已弃用的Carbon.File.FSResolveAliasFile?全部内容,希望文章能够帮你解决在OSX上用Python替换现在已弃用的Carbon.File.FSResolveAliasFile?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1025529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存