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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:27:34+00:00 2026-06-03T19:27:34+00:00

I’m writing a script that has two inputs: a file containing a list of

  • 0

I’m writing a script that has two inputs:

  1. a file containing a list of names,
  2. a different file (can be empty or non-empty)

The purpose of the script is to take the names from the first file, access a certain API and get information on each name, then write that information to the second file. If the second file already contains information, the script reads the last line of the written file, finds that entry in the first file, and then starts from that entry. I’m running into trouble however, when it comes to reading and writing to the second file.

When I set the second file type to “r+” and then do a print on file_two.read(), it shows the contents of the second file. When I do another print on the very next line, it shows the file as empty. As well, when I set the file type to “a+”, it shows the file as completely empty both times, even though I can clearly see the information in the text file is indeed there. Anyone know what’s going on?

Here’s the code segment that handles the writing:

def write_to_file(filename, users):
    pages = range(0, len(users) - 100, 100)
    for page in pages:
        user_names = get_users(users[page: page+100])
        lines = [format_user(user) for user in user_names]
        output_text = '\n'.join(lines)
        with filename as output_file:
            output_file.write(output_text.encode('utf-8'))

and here’s the code segment that calls the above function.

file_one = args.file1
file_two = args.file2
users = read_names_automatic(file_one)
write_to_file(file_two, users)
  • 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-03T19:27:40+00:00Added an answer on June 3, 2026 at 7:27 pm

    When I set the second file type to “r+” and then do a print on file_two.read(), it shows the contents of the second file. When I do another print on the very next line, it shows the file as empty.

    Yes, this is expected behavior. The file pointer is pointing to the end of the file at the end of file_two.read(), so any attempt to read further returns an empty string. For example, check out the following:

    with open("test.txt") as f:
        print f.readline() # => prints the first line, sets file pointer to second line
        print f.read() # => prints everything from the second line on
    

    To reset the file pointer to the beginning of the file:

    with open("test.txt") as f:
        print f.read() # => prints entire file, sets file pointer to end of file
        f.seek(0) # => reset file pointer to beginning of file
        print f.read() # => prints entire file
    

    As well, when I set the file type to “a+”, it shows the file as completely empty both times, even though I can clearly see the information in the text file is indeed there.

    When you open("test.txt", "a+"), the file pointer is set to the end of the file, so any attempt to read will yield an empty string. Indeed, this is what allows you to append to the file. If you file_two.seek(0) and then file_two.write(), the written text will be added to the beginning of file_two.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.