在Python 2.7中创建一个新目录

时间:2017-04-11 04:13:29

标签: 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"

1 个答案:

答案 0 :(得分:1)

使用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"