Tuesday 16 June 2020

Files in Python

Files :

 To store data in permanently:

1.    Files

2.    Database

 #example program on without using files:

a=input("enter any string ");

print(a)

 Above program only process the data, but it will not store the data permanently in the hard disk. Because it stores in RAM (Random Access memory).

To store data we have to go for the following options:

1.    Files

2.    Using data base

 

Files:

#creating a file with name rfile.txt

f=open("rfile.txt","w")

a=input("enter any string:")

f.write(a)

f.close()

 

File definition:

File is a collection of related information which stores in our hard disk permanently.

 

Advantages:

1.    When the data is stored in a file, it is stored in the memory permanently.

2.    Updating becomes easy. For example we add or delete new data to the existing file, we can delete unnecessary data from the file.

3.    This file can be sharable to other modules or other programs.

4.    Huge amount of data can be stored in a file.

 

Types of Files:              

1.    Text files

2.    Binary Files

Test file store the data in the form of characters.

Binary Files stores the data in the form of bytes.  i.e a group of 8 bits.

It can store text file, image file, audio and video.

 

We have to follow some steps to handling the files:

1.    Open a file

Example: f=open("rfile.txt","w")

2.    Read /Write files

f.write(a)

3.    Closing the file

Example: f.close()

 

Example: 2:

#Reading a file from already existing

#Reading a file from already existing

#step1

f=open("rfile.txt","r")

#step2:

a=f.read(9)

print(a)

print(type(a))

#Step 3:

f.close()

"""

Hello I'm

<class 'str'>

"""

Example 3: using readlin() function

#Ex3: Reading a file from already existing

#step1

f=open("rfile.txt","r")

#step2:

##a=f.read(9)

a=f.readline()

print(a)

print(type(a))

#Step 3:

f.close()

"""

Hello I'm rajendra

<class 'str'>

"""

 

File mode

Meaning

w

To write the data into file. If already exists the file, previous data is deleted with ‘w’ mode.

r

To read the data from the file. Actually fp (file pointer) points to the beginning of the file. Where fp is a variable identifier. It acts as a just like a pointer.

w+

To write and read the data from the file. Previous data is deleted.

a

Appending mode. Adding the new data to the existing file at the end of the existing file. Here f or fp points at the end of the file. If file does’nt exist, it creates a new file for writing the data

r+

To read and write the data into a file. Previous data is not deleted

a+

To append and read data of a file.

x

Exclusive mode. If already exits one file, it will not create the file.

 #Ex3: By using looping to read the data entire

#step1: open the file rifle.txt in read mode.

f=open("rfile.txt","r")

#step2:execute the loop to read entire txt file

#a=f.readline()

for i in f:

    print(i)

#Step 3:

f.close()

"""

Hello I'm rajendra

 

I'm from Vijayawada

 

 

 

Python...

"""

Closing a file: f.close()

Syntax: variable.close()

A file which opened should closed using close() method. If a file is opened but we forgot to close the file, that file may be corrupted or deleted.

 

Writing the file to the existing file:

##Example program on appending the file

f=open("rfile.txt","a")

 

#appending the data to the file

f.write("we are going to write and give viva for python")

 

#closing the opened file

f.close()

 

once you execute the above code, go to your directory check the file.

 

f=open("pyt.txt","x")

print(f)

if f:

    print("file created successfully ")

 

"""

It leads to error if already exists.

Otherwise creates new file

"""

Next: Files part 2

Previous: 

Exceptions:

https://youtu.be/TeVaBIo-WQo

 

 

 


1 comment:

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...