0

i am trying 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

BalusC
1.1m376 gold badges3.7k silver badges3.6k bronze badges
asked Jan 8, 2010 at 5:23
2
  • 1
    x = 11; if x > 5? That's always true. What's the point of this? Even with the update, the question makes very little sense. Can you rewrite this question so it does make sense? Could you -- for example -- delete the code that makes no sense? Commented Jan 8, 2010 at 11:37
  • 1
    This question is a great example on how not to ask. You should have asked "how do I check for the content size of a file in python" in the first place - not pasting some arbitrary, partial attempt to do it without telling what you are trying to do. Commented Jan 8, 2010 at 12:05

4 Answers 4

2

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
answered Jan 8, 2010 at 5:26
2

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!

answered Jan 8, 2010 at 5:30
2

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
answered Jan 8, 2010 at 5:39
0

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
answered Jan 8, 2010 at 12:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.