Python Forum
Upgrading from 2 to 3 and having file write problems
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upgrading from 2 to 3 and having file write problems
#1
#!/usr/bin/python

import serial, time, sys, fileinput
 
#open and configure serial port
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=19200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout = .1	
)

#first, clear out buffer
count = 0
while (count < 3):
 count +=1  
 ser.write("\r".encode())

 time.sleep(.1) 

#open file for download
file = open("download.mem", "w+")

#send command to start download
ser.write("1SendEram\r\n".encode())

indata ="" 
Counter = 0
progresscounter = 0
var = 1

while var==1:
  indata = ser.readline()
# print indata
  if indata[0] =="C": #check for first character of "Complete" and exit loop
    break
  if(len(indata) == 0 or indata[0] =="-" or indata[0] == "+" or indata[0] =="T"):
    Counter = Counter + 1	
  else:
    Counter = 0 
    ser.write("\r".encode())
    file.write(indata)
    #LineCount -= 1 
    progresscounter += 1
    progress = progresscounter / 44
    if( progress > 100 ) : progress = 100
    ser.write("OK\r\n")	
	
    print( '\rDownloading: %s (%d%%)' % ("|"*(progress/2), progress)),
    sys.stdout.flush() 
  
  if Counter > 10:
    file.close()
    sys.exit("Unit did not respond. Exiting")
	
print ("\nDownload Complete")  
  
file.close()
sys.exit()
The above code was originally written for Python2.7 and I am upgrading to Python3. I'm slowly working my way down the file (I've corrected various errors as I work my way down) but I'm stuck at the line "file.write(indata)". indata is read from the serial port and I want to write it to the file (download.mem). Python3 throws "TypeError: write() argument must be str, not bytes". This is the file.write statement on line 44

I'm not a total newbie to Python but I kinda am to Python3. I don't see how to cast "indata" as a string so it will write to the file
Reply
#2
try

file.write(indata.decode())
Reply
#3
(May-08-2022, 09:37 PM)Axel_Erfurt Wrote: try

file.write(indata.decode())

Thank you, that worked!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 402 Jan-22-2024, 12:56 PM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 608 Nov-16-2023, 03:41 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 1,495 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,407 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,393 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,083 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,570 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,830 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,424 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Cursor write 3rd file empty paulo79 3 1,860 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020