Generate file with Python dynamic name

0

I'm creating communication socket for some devices with server. I need these devices to always record information in the same file. The file name must be the first few characters within your message.

import socket
import _thread

HOST = '192.168.0.63'
PORT = 5000

def conectado(con, cliente,):
    print ('Conectado por', cliente)

    while True:
        msg = con.recv(1024)
        if not msg: break
        print (cliente, msg,)

        arquivo = open(conectado, 'a+')
        arquivo.write(str(msg))
        arquivo.write('\n')
        arquivo.close()


    print ('Finalizando conexao do cliente', cliente)
    con.close()
    _thread.exit()

tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

orig = (HOST, PORT)

tcp.bind(orig)
tcp.listen(1)

while True:
    con, cliente = tcp.accept()
    _thread.start_new_thread(conectado, tuple([con, cliente,]))

tcp.close()

The device would send ASCII, starting with 8 numbers, which represent its ID. When trying to execute the code I get the following error:

Unhandled exception in thread started by <function conectado at 0x7fd4ed513e18>
Traceback (most recent call last):
  File "/home/ubuntu/PycharmProjects/projeto1/projeto1.py", line 15, in conectado
    arquivo = open(conectado, 'a+')
TypeError: expected str, bytes or os.PathLike object, not function
    
asked by anonymous 26.11.2018 / 20:54

1 answer

-1

The correct to use is:

arquivo = open("log_" + str(msg) + ".txt",'a+')
    
03.12.2018 / 15:33