2014-01-24 3 views
-1

이 파이썬 프로그램의 문제를 해결하는 데 도움이 될 수 있습니까?python 'int'객체를 str 암시 적으로 eroth

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) 

미리 감사드립니다.

+1

어떤 오류가 발생합니까? 추적을 게시하십시오. – thegrinner

+0

os.mkdir (번호) 에있는 "H : \. idlerc \ test.py"파일의 형식 추적 : 암시 적으로 'int'개체를 str로 변환 할 수 없습니다. – 09stephenb

답변

4

int 인수를 지정했습니다.

os.mkdir(str(number)) 
1

os.mkdir 문자열이 아닌 정수를 취이 int 처음 str에 변환해야합니다. 그냥

os.mkdir(str(number)) 

대신 당신이 당신의 스크립트가 충분히 명시해야 실행할 때 예외가 당신이 얻을 것을 참고

os.mkdir(number) 

의 수행

TypeError: coercing to Unicode: need string or buffer, int found 
1

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' 
관련 문제