1

我正在为主桌面制作一个列表,以读取哪些图标是可执行的,哪些不是。我不断收到错误:TypeError: cannot concatenate 'str' and 'list' objects

这是代码示例:是的,我已经尝试了一切,但它只是不想工作。

    list0 = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]
    for filee in fls:
        if filee.endswith(".jex"):
            jkjk = filee
            icon = os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+"/Files/Icon")
            for anything in icon:
                if anything.endswith(".png"):
                    fttts = pygame.image.load("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+"/Files/Icon/"+anything)
                    fttts = pygame.transform.scale(fttts, (48, 48))
                    screen.blit(fttts, (15+aaas, 295+fasdd))
                    print("aaas = "+str(aaas))
                    print("fasdd = "+str(fasdd))
                    fasdd = fasdd + 45

                    if fasdd >= winy-295-63:
                        aaas = aaas + 63
                        fasdd = -280
            jkjk = jkjk+"/Files/Executable"
            jkjk = os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk)
            for exfilepy in jkjk:
                if exfilepy.endswith(".py"):
                    for o in os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+execfilepy):
                        ftttThis[o] = [str(os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+execfilepy)[o])]
                    for i in list0:
                        list0[i] = ftttThis[i]

.jex 是自定义扩展名,不会导致问题。它是列表。

输出:

Traceback (most recent call last):
  File "/Users/kf9z/Desktop/JupeeGUIFiles 2/JUPEE_GUI_WINDOWS copy.py", line 750, in <module>
    top().top()
  File "/Users/kf9z/Desktop/JupeeGUIFiles 2/JUPEE_GUI_WINDOWS copy.py", line 418, in top
    lock().locked()
  File "/Users/kf9z/Desktop/JupeeGUIFiles 2/JUPEE_GUI_WINDOWS copy.py", line 338, in locked
    lock().unlocked()
  File "/Users/kf9z/Desktop/JupeeGUIFiles 2/JUPEE_GUI_WINDOWS copy.py", line 410, in unlocked
    setup().setdesk()
  File "/Users/kf9z/Desktop/JupeeGUIFiles 2/JUPEE_GUI_WINDOWS copy.py", line 191, in setdesk
    for o in os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+execfilepy):
TypeError: cannot concatenate 'str' and 'list' objects

这是 Python 2.7(或 2.x)

4

1 回答 1

0

jkjk在此处更改为列表:

jkjk = os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk)

然后你试图将它连接到一个字符串:

os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk+execfilepy):

如果您想要原始jkjk列表,则将列表分配给一些新变量:

jkjk = jkjk+"/Files/Executable"
lis = os.listdir("/Users/"+getpass.getuser()+"/Desktop/"+jkjk) 
for exfilepy in lis:
     #your code
于 2013-07-06T18:37:08.490 回答