Python List帮助:TypeError:无法连接'str'和'list'对象

时间:2013-07-06 18:33:14

标签: python

我正在为主桌面制作一个列表,以便读取哪些图标可执行,哪些图标不可执行。我一直收到错误: TypeError:无法连接'str'和'list'对象

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

    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)

1 个答案:

答案 0 :(得分: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