Reputation: 107
i have a small program for receiving a txt file from an arduino. the problem is sometimes it prints an empty file. i assume because the receiving part of the code is empty at the time. can someone help me make this code not write to the file when "t" is empty so i can prevent it from writing a blank txt file? thanks
with open('sensData.txt','wb') as f:
while True:
t = conn.recv(20)
print t
if not t:
s.close()
break
f.write(t) #Write To File UNLESS BLANK
Upvotes: 2
Views: 53
Reputation: 1445
you need to try this :
with open('sensData.txt','wb') as f:
while True:
t = conn.recv(1)
print t
if t =='':
s.close()
break
f.write(t)
or you can populate a string and write it at once at the end of the loop
with open('sensData.txt','wb') as f:
receivedData = ""
while True:
t = conn.recv(1)
print t
if t =='':
s.close()
break
receivedData+=t
f.write(receivedData)
Upvotes: 1