pythonrgb

Reading an expanding list from a .txt file and breaking loop when an element is found. Also typing done when the first script is done


I am making a python code where one python script prints all of the rgb values, and another python script is constantly reading it. NOTE: They are both in separate python files! When the first script is done printing all of the rgb values I don't know how to print done when it is finished printing all of the values. Then I am working on the next script, reading the file, and I can't get it to stop even though I put done in the txt file. Please help me.

First File(Types RGB Values)

def generatergbvalues():
    rgb_values = []
    for r in range(256):
        for g in range(256):
            for b in range(256):
                rgb_values.append((r, g, b))
    return rgb_values

def run():
    rgb_values = generatergbvalues()
    
    # Print the RGB values
    for value in rgb_values:
        file = open ('Python/Color/colors.txt', 'a+')
        file.write(str(value))
        file.write('\n')
        print(value)

if __name__ == "__main__":
    run()

This prints the rgb values, everything is working well, but I need it to print done when all of the rgb values are hit.

Second File(Reads the RGB Values)

done = False
def read(done):
    file = open ('Python/Color/colors.txt', 'r')
    data = file.read()
    datalist = data.replace('\n', '/').split('/')
    print(datalist)
    i = "done"
    if i in datalist:
        done == True
while done == False:
    read(done)
    if done == True:
        break

It reads the colors.txt file well, but I am having trouble stopping it after it sees the word 'done'. It just keeps going on forever.

colors.txt(File that has all of the rgb values[Sample])

(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0, 3)
(0, 0, 4)
(0, 0, 5)
(0, 0, 6)
(0, 0, 7)
(0, 0, 8)
(0, 0, 9)
(0, 0, 10)
(0, 0, 11)
(0, 0, 12)
(0, 0, 13)
(0, 0, 14)
(0, 0, 15)
(0, 0, 16)
(0, 0, 17)
(0, 0, 18)
(0, 0, 19)
(0, 0, 20)
(0, 0, 21)
(0, 0, 22)
(0, 0, 23)
done

This is just a sample of the colors.txt when there are 23 values and done in the code.


Solution

  • There is a lot to unpack here. I'll divide this answer into two sections based on the codes you provided. I am assuming you are begginer at python and I will try to structure my answer as such.

    Producing the RGB list

    Improving upon your code without changing it much, it could look like this:

    def generatergbvalues():
        rgb_values = []
        for r in range(256):
            for g in range(256):
                for b in range(256):
                    rgb_values.append((r, g, b))
        return rgb_values
    
    def run():
        rgb_values = generatergbvalues()
        # Print the RGB values
        file = open("Python/Color/colors.txt", "w")
        for value in rgb_values:
            file.write(str(value))
            file.write('\n')
        file.write("done")
        file.close()
    
    if __name__ == "__main__":
        run()
    

    Please especially note the following changes:

    More advanced tips if you want to look into them later:

    Reading the RGB list

    You are overcomplicating this part a lot. If your goal is to read the list and print the values, one by one, you can achieve it with something as simple as:

    def read():
        file = open('Python/Color/colors.txt', 'r')
        data = file.read()
        for rgb_item in data.split('\n'):
            # do something with the data here
            print(rgb_item)
        file.close()
    
    if __name__ == '__main__':
        read()
    

    This will stop after the whole file is read.

    Your code did not stop because done wasn't global variable, because you had typo in the assigment (== is comparison, = is assignment) and because each cycle read the whole file.

    In the case you wanted to read the file line by line AND stop it any time you hit the "done" string, it would look like this:

    def read():
        file = open('Python/Color/colors.txt', 'r')
        data = file.read()
        for rgb_item in data.split('\n'):
            # do something with the data here
            if rgb_item == "done":
                break
            print(rgb_item)
        file.close()
    
    if __name__ == '__main__':
        read()
    

    I feel like you are not quite sure with the concept of opening and closing files here, and I would advise further studying on that.