2014-12-29 2 views
0

특정 조건에서 디렉토리를 만들려면 다음 코드가 있습니다. 이 나에게 라인OSError : [Errno 2] 해당 파일이나 디렉토리가 없습니다. '39'

os.makedirs(analysis_id)

에 오류가 있습니다 실행시

def create_analysis_folder(self, analysis_id, has_headers): 

     path = None 
     if not os.path.exists(analysis_id): 
      os.makedirs(analysis_id)  
     os.chdir(analysis_id) 
     if has_headers == False: 
      path = os.getcwd() + '/html' 
      return path 
     else: 
      os.makedirs('html') 
      os.chdir('html') 
      shutil.copy("../../RequestURL.js", os.getcwd()) 
      return os.getcwd() 

는 오류가 OSError: [Errno 2] No such file or directory: '39'을 말한다. 그러나 나는 프로세서에 디렉토리를 만들고 있는데, 왜 이런 오류가 나옵니까?

+0

는 숫자입니다. 문자열로 변환 해보십시오. –

+1

추적 표시 란 무엇입니까? 'create_analysis_folder'를 실행 한 후에'chdir'을 사용하지 마십시오. 실제로 어떤 디렉토리에 있는지 알 수 없습니다. – Daniel

답변

2

문제는 이미 내 의견에 명시된대로 chdir입니다. 여기에 일어나는 내용은 다음과 같습니다

>>> os.makedirs('a/b/c') # create some directories 
>>> os.chdir('a/b/c') # change into this directory 
>>> os.rmdir('../c') # remove the current directory 
>>> os.makedirs('z') # trying to create a directory in a non-existing directory 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/.../python2.7/os.py", line 157, in makedirs 
    mkdir(name, mode) 
OSError: [Errno 2] No such file or directory: 'z' 

올바른 방법은, 이러한 문제가 처리 :

그것은 당신의 'analysis_id'을 보인다
BASE_DIR = os.getcwd() # or any other path you want to work with 
def create_analysis_folder(self, analysis_id, has_headers): 
    if not os.path.exists(os.path.join(BASE_DIR, analysis_id)): 
     os.makedirs(os.path.join(BASE_DIR,analysis_id)) 
    path = os.path.join(BASE_DIR, analysis_id, 'html') 
    if has_headers: 
     os.makedirs(path) 
     shutil.copy(os.path.join(BASE_DIR, "RequestURL.js"), path) 
    return path 
관련 문제