Displaying the contents of the file - python

I'm having problems with displaying the contents of the file:
def NRecieve_CnD():
host = "localhost"
port = 8080
NServ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
NServ.bind(("localhost",8080))
NServ.listen(5)
print("Ballot count is waiting for ballots")
conn, addr = NServ.accept()
File = "Ballot1.txt"
f = open(File, 'w+')
data = conn.recv(1024)
print('Ballots recieved initializing information decoding procedures')
while(data):
data1 = data.decode('utf8','strict')
f.write(data1)
data = conn.recv(1024)
print("Ballot saved, Now displaying vote... ")
files = open("Ballot1.txt", "r")
print(files.read())
when the program is run the area where the contents of the file are supposed to be shown is blank.

You're writing to a file, then opening the file again and without flushing your first write either explicitly, or by closing the file handle, you're reading a file. The result is you're reading the file before you finish writing it.
f.write(data1)
f.flush() # Make sure the data is written to disk
data = conn.recv(1024)
print("Ballot saved, Now displaying vote... ")
files = open("Ballot1.txt", "r")
Beyond that, it's probably best if you don't keep the file open for longer than necessary to avoid surprises like this:
with open(File, 'w+') as f:
f.write(data1)
data = conn.recv(1024)
print("Ballot saved, Now displaying vote... ")
with open(File, "r") as f:
print(f.read())

Related

How to correctly read bitstreams in Python

Tell me how to write bitstreams correctly
Here is my code, it works, but it seems to me that I am not writing data to the file correctly.
fd = stream.open()
filename = 'file.ts'
while True:
data = fd.read(8192)
file_output = open(filename, "ab")
file_output.write(data)
fd is a data stream, it is replenished and, accordingly, I must read from it until it ends.

write on FTP file without overwrite previous text ftplib python [duplicate]

I'm appending values to a log file every 6th second. Every 30 sec I'm transferring this log to an FTP server as a file. But instead of transfering the whole file, I just want to append the collected data to the file on my server. I haven't been able to figure out how to open the server file and then append the values.
My code so far:
session = ftplib.FTP(authData[0],authData[1],authData[2])
session.cwd("//"+serverCatalog()+"//") # open server catalog
file = open(fileName(),'rb')
with open(fileName(), 'rb') as f:
f = f.readlines()
for line in f:
collected = line
# In some way open server file, write lines to it
session.storbinary('STOR ' + fileName(), open(fileName(), 'a'), 1024)
file.close()
session.quit()
Instead, do I have to download the server file open and append, then send it back?
Above was my question, the full solution is below:
session.cwd("//"+serverCatalog()+"//") # open server catalog
localfile = open("logfile.txt",'rb')
session.storbinary('APPE serverfile.txt', localfile)
localfile.close()
Use APPE instead of STOR.
Source: http://www.gossamer-threads.com/lists/python/python/22960 (link to web.archive.org)

file.write() isn't writing to text file

I'm trying to send a file over a socket. Everything seems to be working properly except for the file not writing properly. I snipped the code down to the major issue but can send the full server and client code if necessary.
if inst == "send":
try:
print ("Receiving...")
l = s.recv(1024)
with open('torecv.py', 'wb') as f:
print ("Writing...")
newFile = l.decode("UTF-8")
f.write(newFile)
f.close()
print ("Done Receiving")
except:
pass
The output returns:
Receiving...
Writing...
and newFile saves the correct data which says to me that it is running f.write is the problem because "torecv.py" is empty.
I'm pretty new to python so go easy on me. Thanks!

python 3.4.3 f.write is not working

The following is Snippet of my program:
with open(completepathname,'wb') as f:
print ('file opened')
print(f)
while True:
print('receiving data...')
print('hello')
data = send_receive_protocol.recv_msg(conn)
#print('hi')
print(data)
if not data:
print('printing1')
break
print('printing')
data1=data.encode()
print(data1)
f.write(data1)#write to file
f.close()
output prints correctly to the console, but if I go open up the file it's blank. If I delete the file and execute my program again, the file is created but still is empty
The following snippet works as expected:
with open('test.txt', 'wb') as f:
while True:
data = 'test-data'
if not data:
break
data1 = data.encode('hex')
f.write(data1)
f.flush()
You should be careful not to .close() a file-like object and then continue to attempt to write to it. If you need to ensure the output is written right away, use .flush().

sending large text file line by line over network with twisted

I am using twisted and python to send some large text file over network, I would like to use UDP and multicast, What solution would be the best to do it, I need sample code cause I am already confused, When I try to do it I get error 24 from python which says too many open files, could you help me to resolve this issue?
here is part of my code:
if (options.upt != None):
print "UPGRADE is initiating"
sourceFile = open(options.upt, "r")
reactor.listenMulticast(1888, UpgradeReciever("Control Listener"), listenMultiple=True)
#with open(options.upt) as sourceFile:
for line in sourceFile:
upgradeSenderObj = UpgradeSender(line, "224.0.0.8", 1888)
reactor.listenMulticast(1888, upgradeSenderObj, listenMultiple=True)
reactor.run()
I have also tried to read the whole file and put in list and then call each element of list (which are in fact lines of my file) by twisted, but still got the similar problem, here is my updated code:
if (options.upt != None):
print "UPGRADE is initiating"
sourceFile = open(options.upt, "r")
reactor.listenMulticast(1888, UpgradeReciever("Control Listener"), listenMultiple=True)
dataContainer = list(sourceFile)
print dataContainer
for i in range(len(dataContainer)):
upgradeSenderObj = UpgradeSender(dataContainer[i], "224.0.0.8", 1888)
reactor.listenMulticast(1888, upgradeSenderObj, listenMultiple=True)
reactor.run()

Categories