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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:50:37+00:00 2026-06-05T23:50:37+00:00

How this file can be closed. Any idea? with open(‘output.txt’,’w’, encoding=’UTF-8′) as output: output.writelines(str(i)+’\n’

  • 0

How this file can be closed. Any idea?

 with open('output.txt','w', encoding='UTF-8') as output:
       output.writelines(str(i)+'\n' for i in range(5))
  • 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-05T23:50:40+00:00Added an answer on June 5, 2026 at 11:50 pm

    The file is automatically closed when you leave the “with-block” or an exception is encountered. This is why it’s the preferred way to open files.

    See this PEP 343 — The “with” Statement for more information on the with statement. The Python “with” Statement by Example gives more information.

    UPDATE:

    Your inability to delete a file opened with with was due to the file having been left open previously (when not using with and not explicitly closeing it). As a test using a different file and with didn’t create a problem.

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

Sidebar

Related Questions

The docs for django.core.files.File imply I can do this: print File(open(path)).url but the File
How can I extract the groups from this regex from a file object (data.txt)?
I am using an XML file to store the values. This XML file can
I have a file named my file.pdf and I can't delete this file with
I want to create this parameter file so that I can send it to
Can anyone help me to add a restriction to this schema file (for OwnerEnglishName)?
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
The UNIX sort command can sort a very large file like this: sort large_file
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I've seen this one-liner perl -lane '$_{$F[0]}+=$F[1]}print$_ $_{$_}for keys%_;{' file here: How can I

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.