2013-06-21 8 views
2

h5py를 사용하여 사전으로 정렬하기 위해 약 1300 개의 h5 파일이 있습니다. 나는 약 250 파일에 내 코드를 실행하면 잘 작동하지만, 더 큰 금액은 나에게 오류 제공 :파이썬 사전이 너무 꽉 차거나 다른가요?

Traceback (most recent call last): 
    File "solution_script.py", line 31, in <module> 
    File "build/bdist.macosx-10.7-intel/egg/h5py/_hl/files.py", line 165, in __init__ 
    File "build/bdist.macosx-10.7-intel/egg/h5py/_hl/files.py", line 57, in make_fid 
    File "h5f.pyx", line 70, in h5py.h5f.open (h5py/h5f.c:1626) 
    IOError: unable to open file (File accessability: Unable to open file)' 

내가 잘못 사전을 설정 한 경우 확실하지 않다 또는 할 수있는 더 좋은 방법이 있는지 이. 누군가가 내가 만든 한 몇 가지 명백한 실수를 볼 수있는 경우

여기 내 코드입니다 :

어떤 도움/조언/통찰력을 크게 감상 할 수
nodesDictionary = {} 
velDictionary = {} 
cellsDictionary={} 
num_h5file = -1; 
for root, directory, files in os.walk(rootDirectory): 
    for file in sorted(files): 
     if file.endswith(".h5"): 
      num_h5file = num_h5file + 1 
       curh5file = h5py.File(file, 'r') 
       nodes_list = curh5file["nodes"] 
       cells_list = curh5file["cells"] 
       velocity_list = curh5file["velocity"] 
       for i in range(0, len(nodes_list)-1): 
        if num_h5file not in nodesDictionary: 
          nodesDictionary[num_h5file] = [curh5file["nodes"][i]] 
          velDictionary[num_h5file] = [curh5file["velocity"][i]]       
        else: 
          nodesDictionary[num_h5file].append(curh5file["nodes"][i]) 
          velDictionary[num_h5file].append(curh5file["velocity"][i]) 
       for j in range(0, len(cells_list)-1): 
        if num_h5file not in cellsDictionary: 
          cellsDictionary[num_h5file] = [curh5file["cells"][j]] 
        else: 
          cellsDictionary[num_h5file].append(curh5file["cells"][j]) 

:

당신은 아마 때 파일을 닫아야합니다

답변

1

작업을 마쳤 으면 한 번에 열 수있는 파일 수에 대한 OS 제한이 있습니다.

h5 파일을 여는 블록의 마지막에 close 문을 입력하면 모든 것이 잘되어야합니다. 나는 당신의 가장 좋은 건 가능하면 적극적으로 사용하지 않는 파일을 닫는 것입니다 @reptilicus에 동의

curh5file = h5py.File(file, 'r') 
...do some stuff 
curh5file.close() 
3

:처럼 뭔가. 당신이 정말로, 당신이 처리 할 수 ​​열려있는 파일의 수를 증가시키기 위해 ulimit 명령을 사용할 수 있어야하지만 - 자세한 내용

에 대한 see this answer

편집 :

당신은 파일을 보장하기 위해 context management를 사용할 수도 폐쇄 오류가 발생하는 경우 :

with h5py.File(file, 'r') as curh5file: 
    ... # Do your stuff 

... # continue other actions 

"with"블록을 종료하면 파일이 자동으로 닫힙니다. 예외가 발생하면 여전히 닫힙니다.

+0

좋아요! 개봉 후 폐점 한 파일이 무엇인지 알 수 있습니다. –