InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python file read write examples

    on Feb 6, 2016

    Reading and writing files in python is very common use case. Here are some examples:

    Write to file using “with open()”

    Here file gets closed automatically outside with block.

    with open("out.txt", "w") as f:
      f.write("hello world\nhello world2\n")
      print f
    print f
    <open file 'out.txt', mode 'w' at 0x7f8210e119c0>
    <closed file 'out.txt', mode 'w' at 0x7f8210e119c0>
    
    Env: Python 2.7.18

    Write to file using “open()”

    Here we need to explicitly close the file handle.

    f = open("out.txt", "w")
    f.write("hello world\nhello world2\n")
    print f
    f.close()
    print f
    <open file 'out.txt', mode 'w' at 0x7fcdc39269c0>
    <closed file 'out.txt', mode 'w' at 0x7fcdc39269c0>
    
    Env: Python 2.7.18

    Read whole file

    with open("out.txt", "r") as f:
      content = f.read()
      print content
      print f
    print f
    hello world
    hello world2
    
    <open file 'out.txt', mode 'r' at 0x7f89e10839c0>
    <closed file 'out.txt', mode 'r' at 0x7f89e10839c0>
    
    Env: Python 2.7.18

    iterate over lines from file

    with open("out.txt", "r") as f:
      for line in f :
        print "=line="
        print line
    =line=
    hello world
    
    =line=
    hello world2
    
    
    Env: Python 2.7.18

    Suggested posts:

    1. Python for loop examples
    2. css z-index – ordering of overlapping elements
    3. How to auto forward gmail from one account to another
    4. Convert mp3 to ogg on Mac using ffmpeg
    5. WordPress – how to add filter to description meta tag
    6. Benefits of using Amazon AWS – EC2
    7. Python pytest – using selenium with PhantomJS
    8. Python string split examples
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress