2010-01-08 66 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