开发者

conditional skip in python if

i am t开发者_Python百科rying for something like this

def scanthefile():
    x = 11
    if x > 5
        """ i want to come out of if and go to end of scanfile """
        print x

     return info

update:

if have to check for the content size of a file. and if the content size is larger than a value say 500 , then i should go to the end of the scanfile


If I understand your question, and I'm really unsure I do, you can just de-indent:

x = 11
if x > 5:
    pass # Your code goes here.
print x


By "go to the end of file", do you mean "seek to the end of file"? Then:

import os

   ...

if x > 5:
  thefile.seek(0, os.SEEK_END)

If you mean something completely different, you'd better clarify!


OK, so to answer your update:

import os

fn = 'somefile.txt'
thefile = open(fn, 'r')

# The next line check the size of the file. Replace "stat(fn).st_size" with your own code if you want.
if stat(fn).st_size > 500:
    thefile.seek(0, os.SEEK_END)
# you are now at the end of the file if the size is > 500


The way I understand the question is that you want to break out of an if-statement, which you can't.

You can however replace it with a while loop:

def scanthefile():
    x = 11

    while x > 5:
        # do something here...
        if CONTENTSIZE > 500:
            break
        # do something else..
        break # Which will make the while loop only run once.

    return info
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜