Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9064117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:10:03+00:00 2026-06-16T16:10:03+00:00

I need to keep calling sync for each line in file.txt until the function

  • 0

I need to keep calling sync for each line in file.txt until the function returns non-zero(that is when it fails). Currently I plan to do the following.

for line in file("file.txt"):
        change=int(line)
        cp_success=sync(change) #check the return value of function sync
        if cp_success!=0 : 
            break               #Try using a break statement

Is there a better way or one-liner?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:10:04+00:00Added an answer on June 16, 2026 at 4:10 pm

    Well, almost in one line (if you allow me to import the itertools module):

    [ x for x in itertools.takewhile(
        lambda line: sync(line) == 0,    # <- predicate
        open("file.txt")) ]              # <- iterable
    

    Example w/o file:

    >>> import itertools
    >>> def sync(n):
    ...   if n == 3: return -1 # error
    ...   return 0
    
    >>> lines = [1, 2, 3, 4, 5, 6]
    >>> [ x for x in itertools.takewhile(lambda x: sync(x) == 0, lines) ]
    [1, 2]
    

    But you really should not obscure things, so why not just:

    with open("file") as fh:
        for line in fh:
            if not sync(int(line)) == 0:
                break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running a function that i need to keep running until i get
I need to keep a colcounter variable inside the loop that will be used
For my application I need to keep track of all files that are touched
I am creating an XML document line by line and need to keep an
Right now I need to keep changing the line below. Is there a programmatic
I need to keep each character passed to .html() as is i.e. not encoded.
I need to keep track of around 10000 elements of an array in my
I need to keep a field in a data-base and update it with a
I need to keep track of how many items are in a node of
I need to keep track of std::set element by saving the iterator returned by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.