• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Files / With statement in Python

With statement in Python

Author: PFB Staff Writer
Last Updated: August 25, 2020

Overview

In Python you need to give access to a file by opening it. You can do it by using the open() function. Open returns a file object, which has methods and attributes for getting information about and manipulating the opened file.

With statement

With the “With” statement, you get better syntax and exceptions handling.

“The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.”

In addition, it will automatically close the file. The with statement provides a way for ensuring that a clean-up is always used.

Without the with statement, we would write something like this:

file = open("welcome.txt")

data = file.read()

print data

file.close()  # It's important to close the file when you're done with it

With Statement Usage

Opening a file using with is as simple as: with open(filename) as file:

with open("welcome.txt") as file: # Use file to refer to the file object

   data = file.read()

   do something with data

Opens output.txt in write mode

with open('output.txt', 'w') as file:  # Use file to refer to the file object

    file.write('Hi there!')

Notice, that we didn’t have to write “file.close()”. That will automatically be called.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Files, System & OS Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Set Difference in Python
  • Convert List of Lists to CSV File in Python
  • Remove Elements From a Set in Python
  • Check if a Python String Contains a Number
  • How to Compare Two Files in Python Line by Line

Copyright © 2012–2022 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us