python'int'对象隐式地指向str errot

python'int'对象隐式地指向str errot,python,Python,有人能帮我解决这个python程序的问题吗?它总是给我一个错误 import os import time import random from random import randint import string count = 0 number = 1 while (count < 50): print(number) os.mkdir(number) count = count + 1 number = number + 1 print(n

有人能帮我解决这个python程序的问题吗?它总是给我一个错误

import os
import time
import random
from random import randint
import string
count = 0

number = 1
while (count < 50):
    print(number)
    os.mkdir(number)
    count = count + 1
    number = number + 1
    print(number)
print("done")
time.sleep(5)
提前感谢。

您正在给mkdir一个int参数。您需要先将此int转换为str:

mkdir接受字符串,而不是整数。照办

os.mkdir( str(number) )
而不是

os.mkdir(number) 
请注意,运行脚本时出现的异常应该足够明确:

TypeError: coercing to Unicode: need string or buffer, int found
os.mkdir需要一个文件路径作为参数

>>> import os
>>> x = 100
>>> os.mkdir(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found
>>> os.mkdir(str(x))
>>> os.path.exists(str(x))
True
>>> os.mkdir(str(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 17] File exists: '100'

它给了你什么错误?发布回溯。回溯最后一次调用:文件H:\.idlerc\test.py,第11行,在os.mkdirnumber类型中错误:无法隐式将“int”对象转换为str
>>> import os
>>> x = 100
>>> os.mkdir(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, int found
>>> os.mkdir(str(x))
>>> os.path.exists(str(x))
True
>>> os.mkdir(str(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 17] File exists: '100'