160 likes | 1.09k Views
Pythonu00a0handlesu00a0filesu00a0differentlyu00a0asu00a0textu00a0oru00a0binary,u00a0andu00a0thisu00a0isu00a0important.u00a0Moreover methods to read and write the data are also mentioned.Thus in this tutorial we have discussed in detail the concept of file handling<br>
E N D
PYTHON HANDLING IN PYTHON • Python also supports file handling and allows users to handle files, i.e. read and write files, along with many other file handling options. • Python handles files differently as text or binary, and this is important. • Each line of code contains a sequence of characters and forms a text file. Each line of a file is terminated by a special charact er called the EOL or the End of the Line characters, such as the {,} or newline characters. It ends the current line, and informs the interpreter that a new line has begun.
• Hence, in Python, a file operation takes place in the following order: 1.Creating a file 2.Read or write into a file 3.Updating a file 4.Deleting a file • File handling in python is confined to text file and binary file.
TEXT FILE • Text file:Modes of text file • Read mode - r • Write Mode - w • Append mode - a • read and write mode - r+ • write and read mode - w+ • append and read mode - a+
OPENING AND CLOSING OF A FILE • Syntax for opening a file: file object = open(file_name [, access_mode][, buffering]) file_name − The file_name argument is a string value that contains the name of the file that you want to access. • access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r). • buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior). • Syntax for Closing a file: fileObject.close()
• Read mode - r If you open a file in read mode and if file exist then it will open the file and places t he cursor at the begining. If you open a file in read mode and if file does not exist then it will give you the File NotFoundError. Only reading is allowed, writing into the file is unsupported operation. • Read and Write mode - r+ If you open a file in read mode and if file exist then it will open the file and places the cursor at the begining. If you open a file in read mode and if file does not exist then it will give you the FileNotFoundError. Reading and writing both are allowed.
• Write Mode - w If you open a file in write mode and if file exist then it will open the file and places the cursor at the beginning of the file after deleting the content of the file. If you open a file in write mode and if file does not exist then it will create a file with the same name and places the cursor at the beginning of the file . Only writing is allowed • write and read mode - w+ If you open a file in write mode and if file exist then it will open the file and places the cursor at the beginning of the file after deleting the content of the file. If you open a file in write mode and if file does not exist then it will create a file with the same name and places the cursor at the beginning of the file . Only writing and reading both are allowed
• append - a If you open a file in append mode and if file exist then it will open the file and places the cursor at the end of the file.If you open a file in app end mode and if file does not exist then it will create a file with the same name and places the cursor at the beginning of the file. Only writing is allowed, reading file the file is unsupported operation, But it will always write the content at the end of the file. • append and read mode - a+ If you open a file in append mode and if file exist then it will open the file and places the cursor at the end of the file. If you open a file in append mode and if file does not exist then it will create a file with the same name and places the cursor at the beginning of the file. Only writing and reading both are allowed.
OPERATIONS ON A BINARY FILE • Video files , audio files ,pdfs , images are all binary files. • All the operations work in the similar way as • Modes in binary file: read - rb write - wb append - ab read and write - rb+ write and read - wb+ append and read - ab+
METHODS TO READ THE DATA • read() - reads the whole content of the file and returns an string object. • read(num of char) - reads the specified no of characters of the file and returns an string object. • readline() - reads one line at a tie and returns a string object. • readlines() - reads all the line form the file and returns list of strings where every trying will be one line
EXAMPLE • f = open('abc.txt', 'r') out_data = f.readlines() print(out_data) print() print(type(out_data)) f.close() • O/P: ['What is Lorem Ipsum?\n', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n', "\\Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n", 'It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \n', 'It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n', '\n', 'Why do we use it?\n', 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. \n', "The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. \n", 'Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).\n', 'If you open a file in write mode and if file exist then it will open the file and places the cursor at the begining fo the file after deleting the content of the file.'] <class 'list'>
METHODS TO WRITE THE DATA • write('str') - takes a string as input and writes it into a file • writelines(list of string)
SEEK FUNCTION • In Python, seek() function is used to change the position of the File Handle to a given specific position. File handle is like a cursor, which defines from where the data has to be read or written in the file. • Syntax: f.seek(offset, from_what), where f is file pointer • Parameters: Offset: Number of postions to move forward from_what: It defines point of reference. The reference point is selected by the from_what argument. It accepts three values: • 0: sets the reference point at the beginning of the file • 1: sets the reference point at the current file position • 2: sets the reference point at the end of the file
TELL METHOD • The tell() method returns the current file position in a file stream. • Syntax: file.tell() • Example: f = open("demofile.txt", "r") print(f.readline()) print(f.tell()) • O/P: Hello! Welcome to demofile.txt 32
THANKS FOR WATCHING!!! • For more follow us on our social media platforms: • Instagram : learnbay_datascience • Facebook : learnbay • LinkedIn : Learnbay • Twitter : Learnbay1