49

I have written this:

    while file.readline().startswith("#"):
        continue

But I suspect the continue is unnecessary? What is the correct syntax for what i'm trying to achieve?

2
  • 1
    When I do this sort of thing I have a generator that skips commented lines and just yields the good lines. Whereas your use case seems to be for skipping the comments at the head of a file. Commented Feb 10, 2013 at 11:18
  • 1
    In this simple example pass and continue are equivalent. I'd favour continue however as it expresses your intent more clearly. Commented Feb 10, 2013 at 11:20

2 Answers 2

81
while file.readline().startswith("#"):
    pass

This uses the pass statement :

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

http://www.network-theory.co.uk/docs/pytut/passStatements.html

Sign up to request clarification or add additional context in comments.

Comments

1

Accepted answer link to pass statement tutorial is no longer working see official docs here:

https://docs.python.org/3/tutorial/controlflow.html#pass-statements

pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.