49

I am trying to append data to a file using numpy's savetxt function. Below is the minimum working example

#!/usr/bin/env python3
import numpy as np
f=open('asd.dat','a')
for iind in range(4):
    a=np.random.rand(10,10)
    np.savetxt(f,a)
f.close()

The error that I got is something about the type of the error

File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1073, in savetxt fh.write(asbytes(format % tuple(row) + newline)) TypeError: must be str, not bytes

This error doesn't occur in python2 so I am wondering what the issue could be. Can anyone help me out?

Meenakshi
  • 591
  • 1
  • 4
  • 3
  • 4
    You have to open your file in binary instead of text mode: `f=open('asd.dat','ba')`. Also consider using the `with` statement to ensure that your file handle gets properly closed in case an error occurs. – cel Jan 05 '15 at 19:59
  • 1
    Possible duplicate: https://stackoverflow.com/questions/14437054/why-should-i-give-savetxt-a-file-opened-in-binary-rather-than-text-mode. – nwk Jan 05 '15 at 20:02

1 Answers1

50

You should open file by binary mode.

#!/usr/bin/env python3
import numpy as np        
f=open('asd.dat','ab')
for iind in range(4):
    a=np.random.rand(10,10)
    np.savetxt(f,a)
f.close()

reference: python - How to write a numpy array to a csv file? - Stack Overflow How to write a numpy array to a csv file?

Community
  • 1
  • 1
user4352571
  • 609
  • 4
  • 4
  • Was looking a long time for this 'ba' option. Thanks. – dorien Mar 30 '17 at 13:13
  • 'b' option didn't work with np.savetxt on my machine. Can anyone confirm it? – Hyunjun Kim Oct 16 '17 at 03:12
  • @HyunjunKim My python (python-3.5, numpy-1.13.3 on Ubuntu 16.04) work well above code. If you try string, append data type (`np.savetxt(f, ["AAA"], "%s")`) – user4352571 Oct 19 '17 at 15:03
  • @user4352571 I thought 'b' option is for writing data in unreadable format by human. But the code above doesn't work without 'b' option. Sorry for my misunderstanding. – Hyunjun Kim Oct 23 '17 at 02:03