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

- 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...

- 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...

- For loop in Python -

Loops: Loops are control structures used to repeat a given section of code  ● a certain number of times or  ● until a particular condition is met.  How do we keep ourselves fit nowadays? We must be doing some exercises ..right?   The steps followed in doing the exercise are repeated many times ..isn’t it? Depending on the fitness level of the person, he can choose to repeat a step 10,20, or even 100 times. Now suppose, you want to write a program to simulate exercise steps. Is it feasible to write the same steps again and again? Also, how many times does it has to be repeated changes from person to person. Looping structures come to our rescue in such scenarios. Duplicating lines can be eliminated by repeating them a required number of times. ‘ for’ loop ‘for loops’ are used when you have a block of code that you want to repeat a fixed number of times. A series of numbers within a given range.  Note :  ● A range has 3 parameters -start, end, ste...