2016-12-30 3 views
5

AttributeError은 파이썬 설명서 (here)에서 예제 코드를 사용할 때 발생합니다. 다음 예제 코드 :with os.scandir()은 AttributeError를 발생시킵니다. __exit__

with os.scandir(path) as it: 
    for entry in it: 
     if not entry.name.startswith('.') and entry.is_file(): 
      print(entry.name) 

결과가 AttributeError이다 os.scandir() 변수를 할당하는 잘 작동

D:\Programming>test.py 
Traceback (most recent call last): 
    File "D:\Programming\test.py", line 3, in <module> 
    with os.scandir() as it: 
AttributeError: __exit__ 

비록. 누군가 내가 누락 된 내용을 말해 줄 수 있습니까?

답변

4

컨텍스트 매니저 지원은 컨텍스트 관리자 아니다 (파이썬 처음 __exit__를로드하려고) 때문에 표시되는 오류가 발생합니다 이전 버전을 사용하려고, 파이썬 3.6에서 추가되었다.

This is stated in its documentationscandir 위해 (바로 아래의 코드는 당신이 본 니펫을) : (강조 광산)

파이썬 3.6을 업데이트하거나, 하나

New in version 3.6: Added support for the context manager protocol and the close() method. [...]

할 수 있다면 당신은 할 수 없습니다 컨텍스트 관리자로 사용하지 마십시오.

2

워드 프로세서 당신은 아마 오래된 파이썬 버전을 실행하는

New in version 3.6: Added support for the context manager protocol

을 말한다.

+0

그게 전부입니다. 나는 여전히 3.5.2.3.6에 완벽하게 작동했습니다. 감사합니다! –

관련 문제