【问题标题】:Creating a new Directory in Python 2.7 [duplicate]在 Python 2.7 中创建新目录 [重复]
【发布时间】:2017-09-06 07:01:27
【问题描述】:

我正在尝试创建一个程序来向新文件夹发送垃圾邮件,但我无法让它工作,我不断收到

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"

【问题讨论】:

  • 错误很明显:msg + amount0 不起作用。试试这个:msg + str(amount0)

标签: python


【解决方案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"

【讨论】:

    猜你喜欢
    • 2018-05-11
    • 2017-07-22
    • 2011-03-20
    • 1970-01-01
    • 2018-08-23
    • 2015-08-30
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多