Error in reading and writing files in Python
<p>I am trying to convert files from one format to other in Python. The current format is DAQ (data acquisition format), which is read in first. Then I use <code>undaq Tools</code> module to write the files to hdf5 format.  </p>

<pre><code>import glob
ctnames = glob.glob('*.daq')
</code></pre>

<p>Following are the few filenames (there are 100 in total):</p>

<pre><code>ctnames
['Cars_20160601_01.daq',
 'Cars_20160601_02.daq',
 'Cars_20160601_03.daq',
 'Cars_20160601_04.daq',
 'Cars_20160601_05.daq',
 'Cars_20160601_06.daq',
 'Cars_20160601_07.daq',
.
.
.
## Importing undaq tools:
from undaqTools import Daq
</code></pre>

<h2>Reading the DAQ files and writing to hdf5:</h2>

<pre><code>for n in ctnames:     
   x = daq.read(n)
   daq.write_hd5(x) 
</code></pre>

<p>Following is the error I got: </p>

<pre><code>C:\Anaconda3\envs\py27\lib\site-packages\undaqtools-0.2.3-py2.7.egg\undaqTools\daq.py:405: RuntimeWarning: Failed loading file on frame 46970. (stopped reading file)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
&lt;ipython-input-17-6fe7a8c9496d&gt; in &lt;module&gt;()
      1 for n in ctnames:
----&gt; 2    x = daq.read(n)
      3    daq.write_hd5(x)

C:\Anaconda3\envs\py27\lib\site-packages\undaqtools-0.2.3-py2.7.egg\undaqTools\daq.pyc in read_daq(self, filename, elemlist, loaddata, process_dynobjs, interpolate_missing_frames)
    272 
    273         if loaddata:
--&gt; 274             self._loaddata()
    275             self._unwrap_lane_deviation()
    276 

C:\Anaconda3\envs\py27\lib\site-packages\undaqtools-0.2.3-py2.7.egg\undaqTools\daq.pyc in _loaddata(self)
    449                         assert tmpdata[name].shape[0] == frame.frame.shape[0]
    450                     else:
--&gt; 451                         assert tmpdata[name].shape[1] == frame.frame.shape[0]
    452 
    453         # cast as Element objects

AssertionError:  
</code></pre>

<h1>Questions</h1>

<p>I have 2 questions:<br>
1. How do I know which of the 100 files is throwing the error?<br>
2. How do I skip the files if they throw the error?</p>

<p>Wrap the <code>read()</code> call in a try/except block.  If you get an exception, print the current filename and skip to the next one.</p>

<pre><code>for n in ctnames:     
    try:
        x = daq.read(n)
    except AssertionError:
        print 'Could not process file %s.  Skipping.' % n
        continue
    daq.write_hd5(x) 
</code></pre>

python