Better way to write so many values in files Python (Lack of memory in a device)
<p>I have a question about file output in Python. 
I was designing a software that reads values from 3 sensors. 
Each sensors read 100 values for 1 second, and between each process, I have to print them in file. </p>

<pre><code>time_memory = [k + i/100 for i in range(100)] # dividing 1 second into 100 intervals
x = [100 elements]
y = [100 elements]
z = [100 elements]
</code></pre>

<p>Below is the code of writing into the file. </p>

<pre><code>for i in range(self.samples):
    self.time_memory[i] = file_time + self.time_index[i] 
    f.write("{0} {1} {2} {3}\n".format(self.time_memory[i], x[i], y[i], z[i]))
</code></pre>

<p>So the result in the file will look like</p>

<pre><code>time_value, x, y, z
time_value, x, y, z
...
</code></pre>

<p>However, when the measurement time is over 8000 seconds, the software stops. 
I think it is due to so many data the device must proceed since the device I am using is kind of an old one. (I cannot change the device because the computer is connected to NI DAQ device.)</p>

<p>I tried to find many alternative ways to change the code above, but I couldn't find it. Is there anyone who can help me with this problem??</p>

<p>One suggestion would be to write the data in binary mode. This should be faster than the text mode (it also needs less space). Therefore you have to open a file in binary mode like this:</p>

<pre><code>f = open('filename.data', 'wb') 
</code></pre>

python file data-acquisition