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

The Archive Base Latest Questions

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

I am currently reading Learn Python the hard way and have reached chapter 16.

  • 0

I am currently reading “Learn Python the hard way” and have reached chapter 16.
I can’t seem to print the contents of the file after writing to it. It simply prints nothing.

from sys import argv

script, filename = argv print "We are going to erase the contents of %s" % filename print "If you don\'t want that to happen press Ctrl-C" 
print "If you want to continue press enter"

raw_input("?") print "Opening the file..." target = open(filename, "w")

print "Truncating the file..." target.truncate()

print "Now i am going to ask you for 3 lines"

line_1 = raw_input("Line 1: ") 
line_2 = raw_input("Line 2: ") 
line_3 = raw_input("Line 3: ") 

final_write = line_1 + "\n" + line_2 + "\n" + line_3

print "Now I am going to write the lines to %s" % filename

target.write(final_write)

target.close

print "This is what %s look like now" %filename

txt = open(filename)

x = txt.read() # problem happens here 
print x

print "Now closing file"

txt.close
  • 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-16T13:18:11+00:00Added an answer on June 16, 2026 at 1:18 pm

    You’re not calling functions target.close and txt.close, instead you’re simply getting their pointers. Since they are functions (or methods, to be more accurate) you need () after the function’s name to call it: file.close().

    That’s the problem; you open the file in write mode which deletes all the content of the file. You write in the file but you never close it, so the changes are never committed and the file stays empty. Next you open it in read mode and simply read the empty file.

    To commit the changes manually, use file.flush(). Or simply close the file and it will be flushed automatically.

    Also, calling target.truncate() is useless, since it’s already done automatically when opening in write mode, as mentioned in the comments.

    Edit: Also mentioned in the comments, using with statement is quite powerful and you should use it instead. You can read more of with from http://www.python.org/dev/peps/pep-0343/, but basically when used with files, it opens the file and automatically closes it once you unindent. This way you don’t have to worry about closing the file, and it looks much better when you can clearly see where the file is being used, thanks to the indentation.

    Quick example:

    f = open("test.txt", "r")
    s = f.read()
    f.close()
    

    Can be done shorter and better looking by using with statement:

    with open("test.txt", "r") as f:
        s = f.read()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to learn about python and is currently reading through a script
I am currently trying to learn Android. Have been reading tutorials and manual for
I'm currently reading Dive Into Python by Mark Pilgrim, and have gotten to the
I'm just starting to learn Python, and I'm currently reading a book that is
I am trying to learn basic python and I can't seem to be able
I'm currently reading Learning Rails 3. I'm working on an example where you can
I am currently reading Developer's Workshop to COM and ATL 3.0. Chapter 3 introduces
I'm currently using twisted's perspective broker on python and I have considered in the
I’m currently trying to learn Objective-C by reading books and online tutorials, also referring
I am an absolute beginner at ASP.NET. How can I learn it better? Currently

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.