Text manipulation error with Python

Asked

Viewed 85 times

-1

I wrote this script to check if the texts are the same, if you do not add the texts you do not have in the other file, but I am receiving this error syntax and I do not understand the reason, is it a logic error? Follows the script: Error: File "write.py", line 3 with open('testee.txt', 'r') as fileOld; ^ Syntaxerror: invalid syntax

try:
        with open('testee.txt', 'r') as fileOld;
        with open('teste.txt', 'r')  as fileNew;

        addFile = fileOld.readlines();
        accFile = fileNew.readlines();
except IOError:
        print("Erro na leitura dos arquivos", IOError);     


key     = "public virtual";
keyStop =  "using";
stop    = false; 

for addlinha in addFile:
        if key in addlinha:
                for acclinha in accFile:
                        if key in accFile:
                                if not addlinha in acclinha:
                                        try:
                                                acclinha.write(addlinha);
                                        except IOError:
                                                print("Houve erros na gravação", IOError);
        else:
                if keyStop in addlinha:
                        stop = true;      
  • 1

    Are you using the with completely wrong, it defines a context manager and, to use it correctly, you need to use the context it generates. Read this question: What’s with no Python for?

  • I used the open in the traditional way, yet the error persists. But vlw I will read the article.

2 answers

0

Your problem occurs due to the use of ;, Python does not use it, it is correct to use the : when necessary, its code has not even been tested whole, because the interpreter is already stopping at:

with open('testee.txt', 'r') as fileOld;

Who is the first ; found

  • I removed all ";" still persists the error, I know that in python, not used, but use by the force of habit, and never had problems. So I’m confused, as you said this is not even passing from this line to execution.

  • You just removed, or also added the : where did you need it? Edit the question and add the new code

  • The use of ; in Python is optional in most cases, but it is not wrong to use. If I want to define two variables, say a and b, along the same lines, I can do a=1;b=2;.

-2

Follow the code with the changes:

try:
        with open('testee.txt', 'rw', ) as fileOld
                with open('teste.txt',  'rw')  as fileNew

        addFile = fileOld.readlines()
        accFile = fileNew.readlines()
except IOError:
        print("Erro na leitura dos arquivos", IOError)     


key     = "DbSet"
keyStop = "using"
stop    = false

for addlinha in addFile:
        if key in addlinha:
                for acclinha in accFile:
                        if key in accFile:
                                if not addlinha in acclinha:
                                        try:
                                                acclinha.write(addlinha)
                                        except IOError:
                                                print("Houve erros na gravação", IOError)
        else:
                if keyStop in addlinha:
                        stop = true                                            
  • Friend, you omitted the : at the end of the two

  • @Fourzerofive in fact were the two points, but in the next line gave another error, anyway, thanks for the help, now it’s easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.