Python Forum

Full Version: Go to line starting with and variable not detected
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, how can I go a line starting with a precise word ? After my first condition, I need to go to the line starting with "/22/" (I have to browse my file in different directions) and add new_name to the new_line, but the result of my first condition isn't persisted through the code...

for fichier in os.listdir("result/"):
    if not fnmatch.fnmatch(fichier, 'file_0.txt'):
        with fileinput.input(["result/"+fichier], inplace=1) as file_X :
            # file_X.write('\r\ncoucou')
            print("---------------------------------")
            for line in file_X:
                line = line.strip('\n')
                if line.startswith("/54/"):
                    split = line.split("/")
                    num = split[2]
                    # print(num)
                    for pj in pjCSV:
                        if num in pj:
                            ...
                            new_name = "jacques"
                            sys.stderr.write(new_name + "\r\n")
                if line.startswith('/22/'):
                    new_line = line + new_name # new_name is not detected
                    print(line.replace(line, new_line))
                    continue
                print (line.rstrip('\n')) # ecriture de toutes les lignes
 
            break
I don't understand. You're saying the result of the first condition is not persisted. But isn't the result new_name? That appears to be persisted. Or are there cases where you don't find a new name, so you want to skip the bit with '/22/'?

Why do you have to go back and forth through the file? That's not easy in Python, so you'd be better off putting more persistence into your code to remember information it might need later, if you can.

Edit: If you really need to go back and forth, and the file is not too large, pull all the lines at once into a list. Searching back and forth through the list is much easier.
new_name is supposed to be used in the second condition (new_name can not be empty or null), but when I try to use new_name in the second condition, i have this

NameError: name 'new_name' is not defined
In your code, it seems that new_name is only defined if some conditions have occurred: a line starting with /54/ is needed before, and num in pj needs to be satisfied, perhaps other conditions that we don't see. The error probably means that some of these necessary conditions failed. You could perhaps write
new_name = 'DEFAULT_NAME'
at the top of the file to see if this name is used.
the name used is
new_name = 'DEFAULT_NAME'
but i need the new_name to be the new_name from the if. I believe that because the "/54/" line is place after the "/22/" line, this could cause the issue, how can i fix it ? (When I read the /54/ line, i need to go back to find the /22/ line)
I found a temporary solution, I've made several with fileinput.input, so that my first is looping on /54/ and the second is looping on /22/