Python Forum
read the first line in my script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read the first line in my script
#1
could someone help me write the proper code for reading the first line of a text file in c:\desktop\ptyhon\log.txt
here is the partial code that should be relevant to my question
def thread_loop():
    secretcode = "oaixxxxxxxxxxxxjxjxjxjxj"
    HOST = '0.0.0.0'  # Standard loopback interface address (localhost)
    PORT = 58900        # Port to listen on (non-privileged ports are > 1023)

    log_path = os.path.dirname(os.path.realpath(__file__)) + "\log.txt"

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        while True:

            if stop_threads: 
                break
            
            conn, addr = s.accept()
            with conn:
                conn.sendall(str.encode('test_right_here'))  ######right here is where i want to read the first line from c:\desktop\log.txt
                while True:
                    try:
                        data = conn.recv(1024)
                        
                        if not data:
                            break
                        data_str = data.decode('utf-8')
                        conn.close()

#more script below..but too long to paste
Reply
#2
FYI: Code windows are scrolled when too large to display at once, so you only have to worry that a script is too large when very large.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 781 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,353 Feb-13-2022, 07:13 PM
Last Post: menator01
  Help, a script line is skipped kucingkembar 5 1,391 Dec-29-2021, 07:34 PM
Last Post: deanhystad
  How to skip to a different line while importing a different script kat_gamer 2 2,208 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  Read characters of line and return positions Gizzmo28 2 1,975 Nov-04-2020, 09:27 AM
Last Post: perfringo
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 2,027 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,119 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Make the script read from any directory falahfakhri 2 2,111 Jun-15-2020, 02:18 PM
Last Post: falahfakhri
  Read all csv files, and store the last line from each folder SriRajesh 4 5,454 Jan-17-2020, 12:13 AM
Last Post: SriRajesh
  How to read text file line by line SriRajesh 1 1,891 Nov-05-2019, 01:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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