The next topic that we need to discuss is about python. Like C, Python File operations. Let us discuss one by one in detailed.
Before going to discuss the various File I/O operations . So ,let us first discuss
A file is some information (or) data, that stays in the computer storage devices. python supports two types of files namely text file and binary file. Text files are the simple files whereas the binary files contain the binary data. Both humans and machines can read the text files. Whereas the binary files can be read by the only computer.
So in learn python code, we have seen what is meant by a file? Now let us have a look over python file operations
As said above, python allows you to create the file. With open() we can create a files. This function takes three arguments. The file that you want to open and the kind of operation (or) permission, you want to do it on the file and buffer.
When the buffering value set to zero, no buffering takes place. But if it set to one, while accessing the file, buffering the file. Usually buffering action takes place if the buffering value greater than one . (with the indicated buffer size). If it is negative , the buffer size is system default.
lt can be done through the following syntax:
f = open(file_name, access mode, buffering)
f = open(itguru.txt, w+)
Here, in the above example, ‘W’ stands for write mode and ‘+’ is used to create the file, if it does not exist in the library.
The other operations that can be done with the file are ‘r’ for reading and ‘a’ for append. And we use the ‘+’ sign to create the file if it does not exist in the directory.
So now we have created the file. Now its time to know
As shown in the example, above since we have created the file, its time to add the data into the file. So as of now, I would the add first 10 number (1 to 10) in a file. So to use this concept, we use a loop.
f = open(“balajee.txt”, “w+”)
for i in range(10):
f.write(” %d\r\n” % (i+1))
This would store the file as
So now we have opened and written the data into the file right, now its time to close the file.
We can close the file with the following syntax
And other this, we can perform some more operations on the file.
|file.mode||It returns the file of the mode in which it is opened|
|file.name||It returns the file name|
|file.softspace||It returns false if space is explicitly required with print. Otherwise it true.|
|flush()||It helps to write the buffer of the file system|
|read()||To read the ‘n’ characters from the file.|
|readline(n== -1)||To read and return one line from the file (if specified, it returns the at most ‘n’ bytes|
|readlines(n=-1)||To read and return the list of lines from the file. (if specified, it returns atmost ‘n’ bytes (or) characters.|
|Writable||If the file system can be written , it returns true|
|write(s)||To write string s to the file and returns the number of characters written.|
|Writelines(lines)||To write the list of lines to the file|
So till now, we have discussed how to create, insert the data and close the file. But rather than this python supports some more file operations .let us discuss one –by –one in detailed.
To get in-depth knowledge on Python you can enroll for live Python Online Training by OnlineITGuru with 24/7 support and lifetime access
|r||Opens the file in reading mode|
|r+||Opens the for reading as well as writing. Here the file pointer is placed at the beginning|
|rb||Opens the file in reading mode but in binary format|
|rb+||Open the file for both readings and writing the file in binary format. The file is placed at the beginning of the file|
|W||Opens the file for writing. It overwrites the file if the file exists|
|W+||Opens the file for both reading as well as writing. If the file already exists, it opens the file. And it creates a file for reading and writing|
|Wb||Opens for writing the file. But in binary format only|
|Wb+||Opens the file for both reading and writing in the binary format. If the file already exists it overwrites the file. And if the file does not exist it creates a new file for reading and writing.|
|a||Opens the file for appending. It does not overwrite the file, but just add the data in the file. And if there is no file, it just creates the file|
|a+||Opens the file for both reading as well as appending. It the file exists, the file pointer is at the end of the file. And if the file does not exist, it creates the file for editing|
|ab||It opens the file for appending, but in binary format|
|ab+||It opens the file for both reading and appending in a binary format. If the file exists, at the end the file pointer is placed. And if the file does not exist, it creates the new file for editing|
Other than reading, writing, and closing, we can perform some file operations.
|File position||tell()||Informs the current position of the file.|
|File position changing||Seek(offset[,from])||This method changes the current file position. Besides the offset here indicates the number of bytes to the move. By default it is set to zero
From – 0- the beginning of the file
From -1 – current position of the file
From-2 – end of the file
|renaming||os.rename(current_file_name, new_file_name)||Renames the current file name with the new file name|
|removing||os.remove(file_name)||Removes the desired file|
|Directory creation||os.mkdir(new_file)||This command creates the directory|
|Working dir||os.getcwd()||This method displays the current working directory|
|Removing directory||os.rmdir(‘dirname’)||This method removes the directory. It takes the file name as an argument|
|Change directory||os.chdir(‘new directory’)||This method is used to change the current directory|
Hope you got a little idea regarding the file operation. And if you practice those commands of your own, you will get an idea. Feel free to clarify your doubts at python online education
Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.
Days were moving very quickly. With days, technology is moving simultaneously. Today technology has bot the advantages and disadvantages. Moreover, today data is the heart of any company.
Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.
Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.
Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.
Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).