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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,347 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 997 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 1,077 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,627 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,406 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 1,670 Jan-22-2024, 12:56 PM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 2,699 Nov-16-2023, 03:41 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 5,435 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,982 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 25,925 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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