Python File IO Basics

You must have noticed that till now we have been learning one new concept per tutorial. For some important concepts like loops we had to allocate two tutorials so we can grasp the concept of both loops (for and while) separately. But now in the case of file we have allocated next five tutorials (excluding the exercise and their solutions). So, from this you can take a hint that how important file handling is in programming.

            In this tutorial we are not getting into files in detail, instead we are discussing the basics of file and its modes in a theoretical manner. In computer terms, “a file is a resource for saving data and information in computer hardware”. A file is stored in the form of bytes in hardware. A file is opened in the RAM, but it is stored in the Hardware because hardware is non-volatile i.e. it stores its data permanently. On the other hand, RAM is volatile, it loses its data when the system is shut down.

Unlike C or C++, file handling in python is relatively easy and simple.  Python treats file differently as text or binary and this is important. There are two types of files that we normally encounter in our computer daily. The first one is text file and the second one is binary file. We can understand by the name of text file that it must contain text in it. The extension for text file is .txt. All other forms of files are mostly binary even a .doc file, that we open in Microsoft Word is a binary file because it requires a special software for accessing it.

The second sort of files are binary files. They are almost all the other files that we come in contact with while using our computer. These files include images, pdf’s, excel files etc.

  Modes of opening file in Python:

            There are many modes of opening a file in Python, unlike other languages Python has provided its users a variety of options. We will discuss seven of them in this tutorial.

  • r : r mode opens a file for read only. We do not have the permission to update or change any data in this mode.
  • w : w mode does not concern itself with what is present in the file. It just opens a file for writing and if there is already some data present in the file, it overwrites it.
  • x : x is used to create a new file. It does not work for an already existing file, as in such cases the operation fails.
  • a : a stands for append, which means to add something to the end of the file. It does exactly the same. It just adds the data we like in write(w) mode but instead of overwriting it just adds it to the end of file. It also does not have the permission of reading the file.
  • t : t mode is used to open our file in text mode and only proper text files can be opened by it. It deals the file data as a string.
  • b : b stands for binary and this mode can only open binary file, that are read in bytes. The binary files include images, documents or all other files that require specific software to be read.
  • + : In plus mode we can read and write a file simultaneously. The mode is mostly use in cases where we want to update our file.

 

Code file as described in the video

# list1 = [ ["Harry", 1], ["Larry", 2],
#           ["Carry", 6], ["Marie", 250]]
# dict1 = dict(list1)
#
# for item in dict1:
#     print(item)
# for item, lollypop in dict1.items():
#     print(item, "and lolly is ", lollypop)
items = [int, float, "HaERRY", 5,3, 3, 22, 21, 64, 23, 233, 23, 6]

for item in items:
    if str(item).isnumeric() and item>=6:
        print(item)

Comments(11)

HarshCoding 10 months, 2 weeks ago
Harry bhai plz is vedio ki coding (Code file as described in the video) paste kar dijiye
7shekhar7 3 weeks, 1 day ago
#files IO basics """ "r" = open file for reading - default mode "w" = open file for writing "x" = creates file if not exist "a" = add more content to a file like append "t" = text mode "b" = binary mode "+" = write and read mode """
PAditya 1 month, 4 weeks ago
Sir there is no code present here Is it my browser's error or you haven't updated the website yet If not please update kar do sir We really need the source code
Vivek-Mishra 1 week, 3 days ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::| Full Notes and Source Code ::::::::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes
Yuvraj 1 month, 3 weeks ago
hello harry bhai the code given by you does not match the code mentioned in the video plz help!!!
sushil2002 1 month ago
print(func1.__doc__)
sushil2002 1 month ago
print(func1.__doc__)
medhansh123 3 weeks, 3 days ago
Sir Please Update the codes on your website
7shekhar7 3 weeks, 1 day ago
7shekhar7 3 weeks, 1 day ago
code for this video (follow me on insta: 7shekhar7 ) #files IO basics """ "r" = open file for reading - default mode "w" = open file for writing "x" = creates file if not exist "a" = add more content to a file like append "t" = text mode "b" = binary mode "+" = write and read mode """
Vivek-Mishra 1 week, 3 days ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::| Full Notes and Source Code :::::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here

Course Content