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
  How to skip to a different line while importing a different script kat_gamer 2 480 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  Read characters of line and return positions Gizzmo28 2 509 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 629 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 1,455 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Make the script read from any directory falahfakhri 2 796 Jun-15-2020, 02:18 PM
Last Post: falahfakhri
  Read a CSV file only from the 4th line/row asaforen7 8 1,626 Apr-16-2020, 01:49 AM
Last Post: Larz60+
  Read all csv files, and store the last line from each folder SriRajesh 4 3,276 Jan-17-2020, 12:13 AM
Last Post: SriRajesh
  How to read text file line by line SriRajesh 1 768 Nov-05-2019, 01:51 PM
Last Post: snippsat
  Read each line, replace string and save into a new file igormonteiro 2 1,378 Sep-15-2019, 01:24 PM
Last Post: buran
  When I read csv file i am getting b'Skipping line messages GuJu 4 5,243 Mar-07-2019, 11:47 AM
Last Post: GuJu

Forum Jump:

User Panel Messages

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