Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

File operations Python

File:

     A file stores related data information, settings or command in secondary storage device like magnetic disks, magnetic tape and optical disks.

     A file can be a sequence of bits, bytes, lines on records depending of the application to create it.

File types:

     A file is a collection of contiguous data. It might be a picture, video, text or even set of records

Ways to access the file:

The content of file can be accessed by

1) Sequential access

2) Direct/Random access

3) Indexed sequential access

Sequential access:

          It is the most basic approach. The content of the file are accessed in sequential order for example text files the line are read one after another

Direct/Random access:

        Read the content of the file from any point of the file. It is also called direct access because the desired content are read directly by specifying the address

Indexed sequential access:

      This mechanism is combination of both sequential and random access. An index is created for the content of the file. The index can be accessed or searched for desired data in sequential manner

Opening and Closing files:

Before reading or writing into file we first need to open the file. This returns the respective file as object and allow us to deal with it

file = open("filename.txt","r")

We use the function open to open a new file. As parameters we need to define the file name and the access mode

Access modes:

Whenever we open file in python we must use certain access mode. An access mode is a way which we can access the mode. Following table gives a quick overview.

access mode
letter access mode
r Reading and Writing
r+ Reading and Writing
rb Reading Binary File
rb+ Reading and Writing Binary File
w Writing
w+ Reading and Writing
wb Writing Binary File
wb+ Reading and Writing Binary File
a Appending
a+ Reading and Appending
ab Appending Binary File
ab+ Reading and Appending Binary File
Closing file:

When we no longer need a opened file then we must close the file. Python done this automatically but it is considered good practice to close files.

file = open("filename.txt","r")
file.close() 

With statement:

We can open and close files effectively by using with statement. A with statement open a file, executes the indented code and closes the file afterwards

with open("filename.txt","r") as file:

In next post lets see some example about file operations ..!!!!

Hope you understand if not feel free to comment and share your thoughts



Comments

Popular Posts