내가 파이썬 (언어와 상당히 경험) 및 파일 I/O, 다음과 같은 오류 가로 질러 땜질 해요 :python이이 열린 파일에 쓰지 않는 이유는 무엇입니까?
ValueError: I/O operation on closed file.
은 매우 간단합니다, 그래서이를 확인하기 위해 인쇄 문에 하락 outfile
의 상태가 어떤 내 코드에 무슨 일이 있었보고 : 그래서 여기
import json
data = {'thing1' : 'foo', 'thing2' : 'bar'}
def writeToJSON():
with open('data.json', 'w') as outfile:
print outfile.closed # outputs 'False', as expected
jsonifiedData = json.dumps(data, indent=4, sort_keys=True, separators=(',', ':'), ensure_ascii=False)
outfile.write(unicode(jsonifiedData)) # trace identifies this line as the issue
writeToJSON()
들여 쓰기가 깨끗한 것 같다이야?
파일은 런타임에 열리는 것처럼 보이지만, 해석기는 많은 양의 파일을 전달한 후 즉시 파일에 쓰는 것에 대해 불평합니다.
감사합니다.
편집 :
추가 된 전체 파일.
또한,이 blurp.py
를 실행하는 완전한 출력 : 귀하의 측면에서 문제가 무엇
False
Traceback (most recent call last):
File "blurp.py", line 11, in <module>
writeToJSON()
File "blurp.py", line 9, in writeToJSON
outfile.write(unicode(jsonifiedData))
ValueError: I/O operation on closed file
추적 오류를 표시 할 수 있습니까? –
내 리눅스에서 이것을 체크하고, 모든 것이 괜찮은 것 같다. – wanderlust
@wanderlust, hm ... 필자는 macOS에서 Python 2.7.13을 사용하고있다. 아마 시스템 일 이겠지만 ... 나는 내 리눅스 머신에서도 이것을 시도 할 것이다. – nrebhun