2016-08-21 107 views
1

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

我试图解决它,但我的蟒蛇技能不是很强。

目前,我强制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 
+4

'join'来自您的最终导入语句:'from os.path import dirname,exists,join'。它用于组合路径段。有关更多信息,请参阅os.path上的官方python文档。 'datadirs = [join(d,'retext')for d in datadirs]'是一个[list comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) 'datadirs'中的目录路径与路径段''retext''。你基本上是得到'datadirs'中每个目录路径的retext子文件夹。 –

回答

1

这是os.path.join()

os.path.join(path, *paths)

加入一个或多个路径组件智能化。返回值是路径和*paths的任意成员的连接,在除最后一个非空部分之后,只有一个目录分隔符(os.sep),这意味着如果最后一部分为空,结果将仅在分隔符中结束。如果某个组件是绝对路径,则所有先前的组件都将被丢弃,并从绝对路径组件继续加入。

在此输入:

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循环中被覆盖,因此除非找到正确的路径,否则它将被覆盖。