A file is an external storage on the hard disk from where data can be stored and retrieved. Python supports reading data from files and writing data to the files. In this part we will be dealing with operations like
- Open a file
- Perform read and write
- Close the file
Opening a file
To open file built-in function open() is used. It returns an object of File which is used with other functions.Syntax:
- file_name refers to the file which you want to open.
- access_mode specifies the access_mode in which the file has to be open, i.e., read, write, append, etc.
- buffer represents that buffering is performed or not, if buffer value is 0 then no buffering is performed and when buffer value is 1 then line buffering is performed while accessing the file.
|r||Opens a file for reading|
|rb||Opens a file for reading only in binary format.|
|w||Opens a file for writing only. Overwrites the file if the file exists.|
|wb||Opens a file for writing only in binary format.|
|a||Opens a file for appending. It does not overwrite the file just add the data in the file and if file is not created then it creates new file|
|ab||Opens a file for appending in binary format.|
Writing into the File
write() method is used to write a string into the file.
Reading from the File
read() method is used to read data from the File.
print kOutput :
It is used to close a file. For this purpose, close() function is used.
So, for instance, we need to the file after we are done with our file operations. We do that by following method.
Methods in File Handling
There are different methods are used which are as follows:
- rename() : This is used to rename a file.
- remove():This method is used to delete a file.
- chdir(): This method is used to change the current directory.
- mkdir() : This method is used to create a directory.
- rmdir() : This method is used to remove the directory.
os.rmdir(“abc”) #where abc is a directory name which you want to remove
- getcwd()– This method is used to show the current working directory.
Various file methods
|close()||To close an open file. It has no effect if the file is already closed.|
|flush()||To flush the write buffer of the file stream.|
|read(n)||To read at most n characters form the file. Remember that it reads till end of file if it is negative or None|
|readline(n=-1)||To read and return one line from the file.|
Remember that it reads at most n bytes, if specified.
|readlines(n=-1)||To read and return a list of lines from the file.|
Remember that it reads at most n bytes/characters if specified.
|seek(offset,from=SEEK_SET)||It changes the file position to offset bytes, in reference to from (start, current, end).|
|tell()||It returns the current file location.|
|writable()||It returns True if the file stream can be written to.|
|write(s)||To write string s to the file and return the number of characters written.|
|writelines(lines)||To write a list of lines to the file.|
That is how we deal with files in Python. Here, we have also discussed different file methods which will be helpful while working on real-world projects. For beginers, a lot of interview questions can be raised on this topic. Prepare yourself for the best via our Python Interview Questions.
So far, we have discussed the basics in our Python online tutorial, which are the building blocks to write a python program.
Now let us go ahead and discuss how to make our program more robust and secure from potential failures.Click on the next tutorial to find more.
Click on the next tutorial to find more or check how to get Python Certification online.