FileNotFoundError: [Errno 2] No such file or directory: 오류 해결방법

CODEDRAGON Development/Python

반응형

 

 

 

오류 메시지

FileNotFoundError: [Errno 2] No such file or directory:

 

 

 

오류 원인

설정되어진 작업경로를 기준으로 파일 찾기 때문에 오류가 나옵니다.

 

 

 

해결방법

VS Code "EXPLORER"에서 작업 경로 지정한 경우 해당 작업 경로를 기준으로 해당 코드가 있는 경로 까지 다시 지정해야 합니다.

 

,

os.getcwd() 메소드를 실행하면 VS Code "EXPLORER"에서 작업 경로 지정까지만 나옵니다.

 

파일을 정확하게 오픈하기 위해서는 절대경로를 지정해 주거나 작업경로를 현재 사용하는 경로로 지정해 주어야 합니다.

 

# VSCode에서 - 작업 디렉토리 변경하기

import os

# Get the current working directory (cwd)

currentPath = os.getcwd()

 

# print path

print(currentPath)

# change path

os.chdir(currentPath+"\\ch-Project\\01")

 

 

 

 

현재 작업 디렉토리 / 디렉토리 변경

 

# 현재 작업 디렉토리 / 디렉토리 변경

import os

 

# Get the current working directory (cwd)

currentPath = os.getcwd()

 

# print path

print(currentPath)

 

# change path

os.chdir(currentPath)

 

# Get all the files in that directory

files = os.listdir(currentPath) 

print("Files in %r: %s" % (currentPath, files))

 

 

 

 

 

반응형