2010-01-08 65 views
0

我试图这样的事情条件跳转如果

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,那么我应该去扫描文件的末尾

+1

'X = 11;如果x> 5'?这总是如此。这是什么意思?即使是更新,这个问题也没有多大意义。你可以重写这个问题,所以它有道理吗?你能否 - 例如 - 删除没有意义的代码? – 2010-01-08 11:37:28

+1

这个问题是一个很好的**例子,如何不问。你应该首先问“如何检查python中的文件的内容大小” - 不要粘贴一些任意的,部分的尝试去做,而不会告诉你正在做什么。 – Kimvais 2010-01-08 12:05:27

回答

1

如果我理解你的问题,而且我真的不确定我做什么,那么你可以取消缩进:

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

“到文件末尾”,你的意思是“寻找到文件的结尾”?然后:

import os 

    ... 

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

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

2

OK,所以回答你的更新:

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 
0

我明白这个问题的方法就是要打破一个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