Featured Post

BUILD A TODO LIST IN PYTHON

Image
Build a TODO List in Python Overview The objective is to build a todo list to which we can add items, remove the added items, edit the items and even if required, delete out the entire list itself. We will learn here step by step, how to build the todo list with the above  CRUD (Create, Remove, Update/edit, and Delete ) features.  This is a CRUD list  app  that we are building as we can Create, Remove, Update items in the list and Delete the entire list itself. We will be using Python3 to build this Project. Importing os and time We import time to let the screen pause for a while before clearing  and we import os to clear the screen . Initializing the list We initialize the todo list with an empty list as there are not items added yet to the list and this is represented in the code aby using an empty square braces   toDoList = [] Defining the print list  The def keyword is used to define a function in python. In the below code, we define the function called printList(). When we define

Working with files in Python

WORKING WITH FILES IN PYTHON





INTRODUCTION


In this Python tutorial, you will learn how to work with files, that is, how to read and write files using python programming. Consider the code in source file as shown below. I have used the repl.it code editor to code. Python is a very powerful language and as you will learn in the following passages, how it is possible to open, read, write and make changes to created other files using our python script in the main.py file. For the purpose of this lesson, we will be using several in-built python functions such as open(), read(), write() and append() functions while explaining the two examples provided below to demonstrate how python handles files.
The code editor that I have used for this purpose is the most popular cloud editor Replit  and you can easily find it from the source code that I have included at the end of this post.



Code Example 1


my_file = open("index.html","w") my_file.write("<p> HELLO WORLD, this is a paragraph sample to demonstrate how to read and write index files in python</p>") my_file = open("index.html", "r") contents = my_file.read() print(contents) my_file.close()



 




USING THE PYTHON IN-BUILT FUNCTIONS FOR HANDLING THE INDEX FILE IN HTML



We have several built-in functions in Python that are used for this example. First , we use the open() function to open the index.html file that we created and then we use 'w' as one of the parameters of the open function to write some data inside this file after opening this file. For writing the content, we use the write() function and then key in the text content that we want to write in that file , as shown above we write the hello world text within the open and closed paragraph elements .


As you can see in the code, we use the open() function to open the index file. For reading the contents of any file, we use the read() function and then for printing the contents, we use the print() function. After writing, reading and printing the contents of a file, it is required to close the file by using the close() function as shown above.
The above code is in main.py file


If you open your index.html file, you will find the code that you input in main.py

<p> HELLO WORLD, this is a paragraph sample to demonstrate how to read
and write index files</p></p>

Next we consider another example code to handle files in python.
In this below example, we use python script to read and write content as well as add data from one file to the other file as shown below in the code editor.


Code Example 2

f = open("mydata.txt") contents = f.read() print(contents) f.close() f = open("createdfile.txt", "w") f.write("Some data written by python script. \n") f = open("createdfile.txt", "r") read_content = f.read() print(read_content) f.close() #appending data to file f = open("mydata.txt", "a") f.write("edtech by meera") f = open( "mydata.txt", "r") read_content = f.read() f.close()




 




HANDLING THE IN-BUILT FUNCTIONS TO HANDLE  AND CHANGE CREATED FILES


In this example, I created a file called mydata.txt  Then I input this single line " Hello world. I am testing out files in python." Now, it is upto you how many lines you want to write here, you can write one or two paragraphs as well. I am just writing one line as it is demonstration purpose only. So, now, I have the new file ready to view and it has a name of mydata.txt

Next, I create one more file and name it as createdFile.txt I do not enter any text inside this file directly as I want to enter the script inside this file through my main python file.  I use the built-in open function in python to open this createdFile.txt and use the write function to enter the line "some data written by python script". Now, when I use the open function, I can read the contents of this file and I can cross check by opening the createdFile directly and see that it is filled with text that I entered into my python code in the main.py file.
Now, you must have observed that in the above built-in functions we have used  different modes, namely: 
w  The w mode is for write only function
r  The r mode is for read only function
a The a mode is for adding or appending new data 
So, for appending new data to our file, myData.txt, we use the mode a and write some new script "edtech by meera"
You will be glad to see that this script that we added in the main.py file will be now visible in our myData.txt


CONCLUSION


You have seen two sets of examples of handling files in Python. As you have learnt above, file handling is a very important feature in python script whereby you are able to write content in another created files, using the write() function and read the content of the file by using the read() function and even make changes to these files that you created, by using the append() function to add new data to the existing content !


In the first example, I showed you how to open, read and write data in index files using the python script in the main.py file


In the second example, I showed you how to open, read, write and also append new data to our files using python script in the main.py file.

You can access my source code and then practise more code in the repl.it code editor .
If there are some queries from your end after reading through this lesson, you are welcome to mention that in the comment section below this blog post. 


You may also like to read related guides in Python:

A Simple Guide to Sets in Python

A Simple Guide to Python Tuples

A Simple Guide to Python Lists


Comments

Popular Posts

Build A Random Quote Machine in React

A Simple Guide to Promises in JavaScript ES6

Build A Calculator in React

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Guide to Learn Coding Efficiently and Effectively