SyntaxError, when working with files

I recently started working with files in Python and, when I run this code, it says invalid syntax and highlights if.
try:
    file = open("statistics","r")
    print(file.read(1))

if dontshowagain == 0:
    print("Hello, this is my program to work with some statistics")

Open in new window

Petr LaškevičAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
You should post also the message that shows details of the error. Anyway, here you use try part of the construct without using except. The except part must be there. See https://docs.python.org/3.6/reference/compound_stmts.html#try
2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gelonidaCommented:
As general advice I agree with @pepr:

It's always a good idea to copy paste the exact error message. This can sometimes massively reduce the speed and effort to help you.

In this particular case:
There is nothing wrong with the line 5 and the if statement.

This a little confusing if not being used to it.

In many cases this means, that the syntax error is in the lines before.
Unfortunately Python doesn't tell you the exact error which should have been something like:

"try statement without except". ( as pepr mentioned already)


In this case you have a try statement without an except and when python sees that there is no except, but it sees an if statement as next statement  and that therefore you must have forgotten the except it will complain.

One way to fix this would be
try:
    file = open("statistics","r")
    print(file.read(1))
except Exception:
    print("failed to open and read a file")

if dontshowagain == 0:
    print("Hello, this is my program to work with some statistics")

Open in new window



If you want to be even more specific and detailed you could do something like:
try:
    file = open("statistics","r")
    print(file.read(1))
except FileNotFoundError:
    print(" ERROR: the file you want to open doesn't exist")
except PermissionError:
    print(" ERROR: the file you want to open exists but you don't have permissions to read it")

Open in new window


or if you're lazy or you think that whatever you do in the except section is not really useful you can just remove the entire try / except statement and just write:

file = open("statistics","r")
print(file.read(1))

Open in new window


In this case the program will 'just' abort with a FileNotFoundError or PermissionError

In most cases try  / except with a decent error message and decent error handling can be a good idea.

However if you need to open / read a statistics file and if it doesn't exist you can't do anything useful, then your  except section should do a little more than printing an error message. It should also abort the program, as you have no data to work on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
python3

From novice to tech pro — start learning today.