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

The Archive Base Latest Questions

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

Instead of this: file = open(f) do_something(file) file.close() it’s better to use this: with

  • 0

Instead of this:

file = open(f)
do_something(file)
file.close()

it’s better to use this:

with open(f) as file:
    do_something(file)

What if I have something like this?

if f is not None:
    file = open(f)
else:
    file = None
do_something(file)
if file is not None:
    file.close()

Where do_something also has an if file is None clause, and still does something useful in that case – I don’t want to just skip do_something if file is None.

Is there a sensible way of converting this to with/as form? Or am I just trying to solve the optional file problem in a wrong way?

  • 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-10T09:16:19+00:00Added an answer on June 10, 2026 at 9:16 am

    For Python 3.7 or higher, then you can use the nullcontext for stand-in purposes:

    from contextlib import contextmanager
    
    none_context = contextmanager(lambda: iter([None]))()
    # <contextlib.GeneratorContextManager at 0x1021a0110>
    
    with (open(f) if f is not None else none_context) as FILE:
        do_something(FILE)
    

    For older Python versions, you can build one yourself on-the-fly with an optional None:

    from contextlib import contextmanager
    
    none_context = contextmanager(lambda: iter([None]))()
    # <contextlib.GeneratorContextManager at 0x1021a0110>
    
    with (open(f) if f is not None else none_context) as FILE:
        do_something(FILE)
    

    It creates a context that returns None. The with will either produce FILE as a file object, or None. But the None type will have a proper __exit__


    Another solution would be to just write it like this:

    if f is not None:
        with open(f) as FILE:
            do_something(FILE)
    else:
        do_something(f)
    

    (file is a builtin btw )

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

Sidebar

Related Questions

My code has something like this: HttpFileCollection files Instead of looping through each file
Do you think that C# will support something like ??= operator? Instead of this:
I have a form that looks something like this: <form id=uploadForm name=uploadForm method=post enctype=multipart/form-data>
If I do something like the following: ifstream file; file.open(somefile, ios::binary); unsigned int data;
Let's say I have a model class called Project, but instead of this: class
What is PHP for C# (asuming we open some local (on server) file instead
I know this sounds like something I can google, but the truth is that
I'd like to add new line with text to my date.txt file, but instead
Feels like I'm missing something pretty obvious here but can't see it. I have
Is there any get() function for this instead? {% for key, value in choices.items

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.