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 8372331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:24:11+00:00 2026-06-09T14:24:11+00:00

I am basically trying to update a line in saved files with new updated

  • 0

I am basically trying to update a line in saved files with new updated number but it leaves only one line in the file. It feels like its overwriting over entire file rather than updating it. I looked at other questions here, and although they gave me right module to use I can’t seem to figure out the problem I am having.

unique = 1
for line in fileinput.input('tweet log.txt', inplace=1):
    if tweet_id in line:  #checks if ID is unique, if it is not, needs to update it
        tweet_fields = line.split(';')
        old_count  = tweet_fields[-2]
        new_count = 'retweet=%d' % (int(tweet_retweet))
        line = line.replace(old_count, new_count)
        print line
        unique = 0
if unique == 1:          #if previous if didn't find uniqueness, appends the file
    save_file = open('tweet log.txt', 'a')
    save_file.write('id='+tweet_id +';'+
                    'timestamp='+tweet_timestamp+';'+
                    'source='+tweet_source+';'+
                    'retweet='+tweet_retweet+';'+'\n')
    save_file.close()

I feel like this has a very easy solution but I am clearly missing it.
Thanks in advance!

  • 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-09T14:24:13+00:00Added an answer on June 9, 2026 at 2:24 pm

    I think the issue you’re having is due to your conditional in the loop over the input. When you use fileinput.input with the inplace=1 argument, it renames the original file adding a “backup” extension (by default “.bak”) and redirects standard output to a new file with the original name.

    Your loop is only printing the line that you’re editing. Because of this, all the non-matching lines are getting filtered out of the file. You can fix this by printing each line you iterate over, even if it doesn’t match. Here’s an altered version of your loop:

    for line in fileinput.input('tweet log.txt', inplace=1):
        if tweet_id in line:
            tweet_fields = line.split(';')
            old_count  = tweet_fields[-2]
            new_count = 'retweet=%d' % (int(tweet_retweet))
            line = line.replace(old_count, new_count)
            unique = 0
        print line
    

    The only change is moving the print line statement out of the if block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm basically trying to extract a Mac application from a zip file using QuaZip.
I'm basically trying to make my js and css files dynamic. I've tried a
I'm trying to get php to update a MySQL table using UPDATE statement but
My project is basically like a Reddit feed that updates in real-time. I'm trying
I'm trying to basically trying to separate a specific amount of text from the
I'm basically trying to iterate through a dict and print out the key /
I'm basically trying to create a url friendly query string with user input. The
What im basically trying to do is to confirm the identity of the user
I am basically trying to trigger a function if the footer is inside the
I'm basically trying to convert a Unix timestamp (the time() function) to a relative

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.