gpt4 book ai didi

python - 这个 "join"在做什么?

转载 作者:太空宇宙 更新时间:2023-11-03 16:13:53 25 4
gpt4 key购买 nike

我刚刚通过 pip 安装了 retext。我必须下载它的图标,但我意识到它不起作用(菜单上没有图标),除非我在 retext 文件夹中运行“retext”。

我尝试修复它,但我的 python 技能不是很强。

目前,我强制 icon_path 具有我想要的路径。

#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'

有人可以告诉我这条线是如何工作的吗?

datadirs = [join(d, 'retext') for d in datadirs]

谢谢。

import sys
import markups
import markups.common
from os.path import dirname, exists, join

from PyQt5.QtCore import QByteArray, QLocale, QSettings, QStandardPaths
from PyQt5.QtGui import QFont

app_version = "6.0.1"

settings = QSettings('ReText project', 'ReText')

if not str(settings.fileName()).endswith('.conf'):
# We are on Windows probably
settings = QSettings(QSettings.IniFormat, QSettings.UserScope,
'ReText project', 'ReText')

datadirs = QStandardPaths.standardLocations(QStandardPaths.GenericDataLocation)
datadirs = [join(d, 'retext') for d in datadirs]

if sys.platform == "win32":
# Windows compatibility: Add "PythonXXX\share\" path
datadirs.append(join(dirname(sys.executable), 'share', 'retext'))

if '__file__' in locals():
datadirs = [dirname(dirname(__file__))] + datadirs

#icon_path = 'icons/'
icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'
for dir in datadirs:
if exists(join(dir, 'icons')):
icon_path = join(dir, 'icons/')
break

最佳答案

这是os.path.join() :

os.path.join(path, *paths)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

它是在这里导入的:

from os.path import dirname, exists, join

所以,有问题的行:

datadirs = [join(d, 'retext') for d in datadirs]

[...] 是一个列表理解,它构建 join(d, 'retext') 结果的列表应用于 datadirs 列表中的每个目录。

因此,如果 datadirs 包含:

['/usr/local/test', '/usr/local/testing', '/usr/local/tester']

然后:

[join(d, 'retext') for d in datadirs]

会产生:

['/usr/local/test/retext', '/usr/local/testing/retext', '/usr/local/tester/retext']

设置问题:

icon_path = '/usr/local/lib/python3.5/site-packages/retext/icons/'

它是在for循环中被覆盖的,所以除非找不到正确的路径,否则它将被覆盖。

关于python - 这个 "join"在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com