Skip to main content

- File Operations in Python -

In Dictionary operations, we learned how to maintain a database of countries and capitals.

But did you notice, that once you close the program all the entries you made are lost? 

Next time you run it, you will have to re-anteater all the things again..isn’t it?

Won’t it be good if you can store it and use it later? 

What if, we could write all the Dictionary details into a text file so that you can refer to them later on.

Today we will see how we can read/write into a text file (extension .txt).

 Opening a file :

open(<filename>,<access_mode>

<filename> refers to the name of the file(with extension) to be opened. 

<access_mode> determines the type of operation that is permitted.

Note  The file should be present in the same location as the .py file, else the complete path of the file needs to be given. 


Note: Each line in the text file has a newline character (‘\n’) at the end. You may use strip() to remove this.

 strip() removes the character at the beginning or end of a string. 

Syntax to access a file

 with open(<filename>,<access_mode>) as  < file_object > :

Note: When a file is opened in this method, it automatically closes the file after use.

Write the quote “Why fit in when you are born to stand out?” to a file named quotes.txt.



 Note:  ‘file=’ in print. ’file’ determines where to print the message. Earlier we were using print without it because by default it prints to screen. 

Open the file quotes.txt and see. 

Write another quote “No act of kindness, no matter how small, is wasted” to the same file. 





Note: Open file “quotes.txt” and see how it replaced the earlier quote.

 Write the quote “Why fit in when you are born to stand out?” to the same file in append mode.





 Note: Open file “quotes.txt” to student and show how the quote got appended to the existing file.

 Read the file quotes.txt using readlines and print the quotes one by one. 



Note: Show ‘\n’ at end of each quote in the list







Note: here strip() removes the newline(‘\n’)

 Create a to-do list manually. quote filename to be used is “mytodolist.txt”. Create the file at the same location as to-our program.

 Write a program to display the items in the todo list one by one. 



 Write a program to append items to the to-do list.








Write a program to display the entire thing as a list.



 



Note: All the operations on lists can be performed on this list created above. 

Comments

Popular posts from this blog

- Animating characters using pygame -

If you see closely you can see that the mickey mouse animation is made out of a lot of shapes.  But to create something like this we need to make a very complex code. The other way around it is to use an image, other than the basic shapes, pygame also can show images.  Using images :  Steps to use images in my game: > find the image you want to use  > move it to the directory where you have created your code  > import/load image  To load the image we will first create an object and then use the image. load method to add the image to the object.  here, we have created an object called an image.  We then use the "pygame.image.load" to find the file in the directory ocean.png has to be replaced with the filename of your file.  To place the object on a particular point of the screen we use the blit function.  this function means that we are going to add the image onto the screen at position 0,0 mickey mouse is winking  Usi...

- Object Oriented Programming -

In OOP, we can represent an entity using a class.  A class bundles data (features/properties of entity - color, height, width) and functionality (movement, expression) together.  An instance of a class is known as an object.  Which is the only cartoon series that had predicted that Trump would be elected as the president of the USA and also predicted the coronavirus spread?   Answer: Simpsons.  Simpsons is a very popular animated show. You can build something similar with python.  But have you ever wondered, how Simpsons look the same in all the series? Only his movement looking and, expression change but his main features like color, height, and face look exactly the same all the time.  This is because we create a ‘blueprint’ of Simpsons and whenever we have to simulate him on screen we use this blueprint. This keeps a record of all the features of Simpsons and recreates it whenever we want.  This is where Object-Oriented Programming(OOP) h...

– Sequences, Algorithms and Programs in Python ---

 Before moving forward, Let's learn how to install Python software in your System which is very important for the further understanding of these topics. Imagine, you are a regular customer of Uncle Sam’s Pizza restaurant, can you help Uncle Sam prepare the menu and take a printout of it? Which application will you use for that? You will type the menu using MS word and then take a printout of it. right?  So, you know that we can use MS Word to create documents. Similarly, if you want to write a python program, you need an IDE (Integrated development environment). This IDE helps you to write a python program and then run it so that you can see the result/output of the program.  Steps to install python/IDE in your local system. (Windows /MAC )   Note : Prerequisite for installing python in Windows- Windows 8 or above. go to this website .   1. Click on download python as per your OS. Mac users may download the Mac version. Windows users download depending on 32-bit...