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

  • Home
  • SEARCH
  • 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 7403295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:02:05+00:00 2026-05-29T05:02:05+00:00

Python newbie here. I’m writing a script that can dump some output to either

  • 0

Python newbie here. I’m writing a script that can dump some output to either a file or stdout, depending on the arguments passed to it. When interpreting arguments, I assign either an open‘ed file or stdout to a global variable named output_file, which can be used by the rest of the script to write output regardless of what type of stream was selected. At the very end of the script I close output_file. This is proper to do for a file stream, and though it’s redundant for stdout, my experience with other programming languages suggests that there’s no harm in explicitly closing stdout immediately before the program ends.

However, whenever stdout is used for output (and subsequently closed), I get a “ValueError: ‘I/O operation on closed file.'”. I know this error is not directly produced by my call to close stdout, but occurs after my script returns. My question is: Why does this happen, and is there a way to manually close stdout without triggering it? (I’m aware that I can easily work around the problem by conditionally closing the stream only when a file was selected, but I want to know if/why this is necessary.)

Very simple demonstrative snippet:

from sys import stdout
stdout.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-05-29T05:02:08+00:00Added an answer on May 29, 2026 at 5:02 am

    The problem is that on python-3.2 there’s an attempt at shutdown to flush stdout without checking if it was closed.

    The issue13444 is about this.

    You shouldn’t have this problem in python-2.7 in releases after the fixing patch.

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

Sidebar

Related Questions

python newbie here. I'm writing the code to control an experiment that has multiple
Python newbie here: I'm writing a market simulation in Python using Pysage, and want
Python newbie here. I was trying to troubleshoot an issue with writing a csv
Newbie python here. How can I break out of the second while loop if
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
As a newbie to Python, and mainly having a background of writing scripts for
I'm newbie in Python. I can't understand why this code does not work: reOptions
I'm a newbie to python and the app engine. I have this code that
I'm a newbie to Python and I'm looking at using it to write some
I'm a newbie with a little experience writing in BASIC, Python and, of all

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.