Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Python File

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

What is a file?

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

How to create a file?

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.

Buffering :

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)Ex :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

How to write data into the file?

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.

Ex:f = open("balajee.txt", "w+")for i in range(10):f.write(" %d\r\n" % (i+1))Output :This would store the file as12345678910

So now we have opened and written the data into the file right, now its time to close the file.

How to close the file?

We can close the file with the following syntax

Syn:

f.close()

And other this, we can perform some more operations on the file.

AttributeDescription
file.modeIt returns the file of the mode in which it is opened
file.nameIt returns the file name
file.softspaceIt 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.
WritableIf 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
File Mode operations
ModeDescription
rOpens the file in reading mode
r+Opens the for reading as well as writing. Here the file pointer is placed at the beginning
rbOpens 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
WOpens 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
WbOpens 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.
aOpens 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
abIt 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
 File operations

Other than reading, writing, and closing, we can perform some file operations.

FunctionsOperationDescription
File positiontell()Informs the current position of the file.
File position changingSeek(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 zeroFrom – 0- the beginning of the fileFrom -1 – current position of the fileFrom-2 – end of the file
renamingos.rename(current_file_name, new_file_name)Renames the current file name with the new file name
removingos.remove(file_name)Removes the desired file
Directory creationos.mkdir(new_file)This command creates the directory
Working diros.getcwd()This method displays the current working directory
Removing directoryos.rmdir(‘dirname’)This method removes the directory. It takes the file name as an argument
Change directoryos.chdir(‘new directory’)This method is used to change the current directory
Suggestion

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