File Handling in Python

This is the post in which you will learn about the Files in Python. Basically you can, by using this post learn how to create a file, write data and read data from it !

Screenshot


Things this code will do:
  • Create a Folder Test
  • Changes the Current Working Directory
  • Creates a file as per your given name
  • Stores the Data you give as input
  • Shows the Data you gave as input

The Code:
import os

# Create a Directory and make it as working dir.
os.mkdir("test")
os.chdir("test")
print "current working Dir: ",os.getcwd()
name=raw_input("Give the Name: ")

# Open a file and writing into it
fo = open(name, "w+")
fo.write(raw_input("Give the Data : "))
fo.close()

# Open a file and reading it
fo = open(name, "r+")
str = fo.read();
print "Read The Data :",str
fo.close()


Got problems ?
Ask us anything anytime, we will help you soon !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java