python中的条件跳过

python中的条件跳过,python,if-statement,Python,If Statement,我正在尝试这样的东西 def scanthefile(): x = 11 if x > 5 """ i want to come out of if and go to end of scanfile """ print x return info 更新: 如果必须检查文件的内容大小。如果内容大小大于一个值,比如500,那么我应该转到扫描文件的末尾如果我理解您的问题,并且我真的不确定我是否理解,您可以缩进: x = 11 if

我正在尝试这样的东西

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

     return info
更新:


如果必须检查文件的内容大小。如果内容大小大于一个值,比如500,那么我应该转到扫描文件的末尾

如果我理解您的问题,并且我真的不确定我是否理解,您可以缩进:

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

如果我理解你的问题,并且我真的不确定我是否理解,你可以缩进:

x = 11
if x > 5:
    pass # Your code goes here.
print x
“转到文件末尾”是指“查找到文件末尾”吗?然后:

如果你的意思完全不同,你最好澄清一下

你说的“转到文件末尾”是指“查找到文件末尾”吗?然后:


如果你的意思完全不同,你最好澄清一下

好的,请回答您的更新:

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

好的,请回答您的更新:

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

我对这个问题的理解是,你想要突破一个if语句,而你不能

但是,您可以用while循环替换它:

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

我对这个问题的理解是,你想要突破一个if语句,而你不能

但是,您可以用while循环替换它:

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

x=11;如果x>5
?这总是正确的。这有什么意义?即使有了更新,这个问题也没有什么意义。你能把这个问题改写一下,让它有意义吗?你能——比如——删除那些毫无意义的代码吗?这个问题是一个很好的例子,说明了如何不问。首先,您应该问“如何在python中检查文件的内容大小”——不要在不告诉您要做什么的情况下粘贴任意的、部分的尝试;如果x>5?这总是正确的。这有什么意义?即使有了更新,这个问题也没有什么意义。你能把这个问题改写一下,让它有意义吗?你能——比如——删除那些毫无意义的代码吗?这个问题是一个很好的例子,说明了如何不问。首先,您应该问“如何在python中检查文件的内容大小”——不要在不告诉您要做什么的情况下粘贴一些任意的、部分的尝试。