在Python2.7中创建新目录

2024-05-03 14:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个程序来垃圾邮件新的文件夹,但我不能让它工作,我不断得到

TypeError: cannot concatenate 'str' and 'int' objects 

但是我已经在把字符串转换成整数了

import os
def folderSpam():
    amount = raw_input("Please insert how many folders you want to create: ")
    amount = int(amount)
    path = raw_input("Please insert the directory to create the folders in: ")
    msg = raw_input("Please insert the spam name of the folders")
    for i in range(0, amount):
        amount0 = 0
        if amount0 == amount:
            exit()
        else:
            amount0 = amount0 + 1
            os.chdir(path)
            os.mkdir(msg + amount0)
            print "All folders have been created"

Tags: thetopathininputrawoscreate
1条回答
网友
1楼 · 发布于 2024-05-03 14:27:00

使用str(amount0)进行字符串和int连接。你知道吗

有关详细信息,请查看Python String and Integer concatenation

import os
    def folderSpam():
        amount = raw_input("Please insert how many folders you want to create: ")
        amount = int(amount)
        path = raw_input("Please insert the directory to create the folders in: ")
        msg = raw_input("Please insert the spam name of the folders")
        for i in range(0, amount):
            amount0 = 0
            if amount0 == amount:
                exit()
            else:
                amount0 = amount0 + 1
                os.chdir(path)
                os.mkdir(msg + str(amount0))
                print "All folders have been created"

相关问题 更多 >