Python中的条件跳过if

0 投票
4 回答
5448 浏览
提问于 2025-04-15 17:47

我正在尝试做这样的事情

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,那么我应该直接跳到扫描文件的末尾。

4 个回答

2

你说的“去文件末尾”,是指“定位到文件末尾”吗?如果是这样:

import os

   ...

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

如果你指的是其他完全不同的意思,最好再解释清楚一下!

2

好的,关于你的更新,我来给你解释一下:

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
2

如果我理解你的问题,虽然我不太确定我理解对了,你只需要减少缩进就可以了:

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

撰写回答