0

I am using Python 3.5 64bit Windows OS. I am trying to write floating point values to a file. This is the code

 with open('report.txt' , "ab") as f:
      f.write(b"\n Result Pattern\n")
      f.write('%f' %test_accuracy)

I have tried the new '{}'.format specifier as well. However I get the following error

f.write('%f' %test_accuracy)
TypeError: a bytes-like object is required, not 'str'

So, how do I specify a format for writing digits , when the file mode is binary. Another point is that, I get an error if I try to write without the binary option.

Update: I am aware of basic file writing in python and when to use b. I have this snippet of numpy code where I am writing the array to the file as well.

            np.savetxt(f , test_pat ,
                   header = 'A , B , C , D , E' ,
                   comments='' ,
                   delimiter = ',' ,
                   newline = '\r\n' ,
                   fmt = '%3d')

I get the aforementioned error at this point with error message as: fh.write(asbytes(comments + header + newline)) TypeError: write() argument must be str, not bytes

It is then when I switch the mode to ab strangely the error goes away.

Is the \r\n a problem while trying to have it formatted nicely when opened through a notepad?

Another update:

Similar error and the answer suggest using binary mode! numpy savetxt append mode error

cs95
  • 379,657
  • 97
  • 704
  • 746
Wilbus Fugu
  • 83
  • 2
  • 11
  • `f.write(b'%f' %test_accuracy)` This? You need `b` here too. – cs95 Aug 14 '17 at 04:59
  • 1
    I don't understand, why are you writing in binary mode, when it looks like you want a text-file anyway? – juanpa.arrivillaga Aug 14 '17 at 05:00
  • You need to deiced which file type you want. You currently seem to be trying to mix text and binary. That won't work. Choose one and stick to it. In this case as @juanpa.arrivillaga said, it really doesn't even look like you need binary. – Christian Dean Aug 14 '17 at 05:02
  • Well, what do you know!, That was the first thing I tried , but was getting an 'Unknown format code b' error, but somewhere in my code I had missed adding the 'b' option and that messed it up. Thank you for your lightning fast response. I needed a third eye really to correct my error. My error is solved now. – Wilbus Fugu Aug 14 '17 at 05:04
  • @juanpa.arrivillaga: I did try without the binary option, but I get the 'write() argument must be str, not bytes' error. Strange, it should occur. – Wilbus Fugu Aug 14 '17 at 05:07
  • @WilbusFugu because then you need to not use a binary literal, i.e. leave out the `b'...'` from the literal... you should really try to understand *why* you are getting these errors. – juanpa.arrivillaga Aug 14 '17 at 05:09
  • @WilbusFugu That's because you're opening your file in the wrong mode. Wait, let me write an answer. – cs95 Aug 14 '17 at 05:09
  • @juanpa.arrivillaga: Yes, I do not specify the `b` while opening. Its the standard `with open('file.txt' , 'a') as f`. I have not specified the binary mode in my strings yet I get the `write() argument must be str not bytes` error – Wilbus Fugu Aug 14 '17 at 05:14
  • @ChristianDean uh, yes, *I* understand that. – juanpa.arrivillaga Aug 14 '17 at 05:17
  • @juanpa.arrivillaga: I know what `b` means. Did you read my comments? – Wilbus Fugu Aug 14 '17 at 05:18
  • @juanpa.arrivillaga Oh crap, I think you know I screwed up there :| Let's try that again. – Christian Dean Aug 14 '17 at 05:18
  • @WilbusFugu Remember, you can only mark one answer accepted. – cs95 Aug 14 '17 at 05:23
  • I apologize @Wilbus if my comment seemed condescending. That was not my intention. When I asked "do you understand"? that was not to imply your lack of intelligence. I was simply asking so I could know whether I should provide you with a link to a relevant section of documentation. Regardless, I deleted the comment seeing as you _do_ seem to understand. I meant no ill will. – Christian Dean Aug 14 '17 at 05:41
  • @ChristianDean: Not a problem at all, and I apologize for misinterpreting {will delete my comment) it. I have updated the question with a similar error and using binary mode solved it like in my case.Am only curious if its somehow related to numpy's `savetxt' functionality and not the actual binary and text file semantics – Wilbus Fugu Aug 14 '17 at 05:46

1 Answers1

0

Try :

with open('report.txt', "ab") as f:
    f.write(b"\n Result Pattern\n")
    f.write(b'%f' %test_accuracy) # you need to add b here.
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
  • Welcome @WilbusFugu. If the answer worked for you, please mark it as accepted by pressing the check mark on the left side of the answer, thanks! – Md. Rezwanul Haque Aug 14 '17 at 05:10
  • This answer is a cookie cutout of [my comment](https://stackoverflow.com/questions/45667702/writing-floating-point-numbers-to-a-file-ab-mode-python/45667784#comment78292788_45667702). What more do you have to add to a comment? – cs95 Aug 14 '17 at 07:41
  • How @cᴏʟᴅsᴘᴇᴇᴅ. ? Why you did not answer this question before comment ? Are you confused ? – Md. Rezwanul Haque Aug 14 '17 at 07:47
  • I wrote an answer because I realised the OP's problem was required a deeper level of explanation to solve than just telling them to add `b`. I have mentioned it before, saying `Try this :` and posting code is not a helpful answer for future readers. – cs95 Aug 14 '17 at 07:48
  • But his error is ocurring for only `b`. So, I think post helpful code is better than explain for this question. – Md. Rezwanul Haque Aug 14 '17 at 07:53
  • StackOverflow appreciates answers that are "good" more than those that are "good enough". Keep that in mind. – cs95 Aug 14 '17 at 07:56