In Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing and reading files.
In this tutorial, we will learn
With Python you can create a .text files (guru99.txt) by using the code, we have demonstrated here how you can do this
- We declared the variable f to open a file named textfile.txt. Open takes 2 arguments, the file that we want to open and a string that represents the kinds of permission or operation we want to do on the file
- Here we used "w" letter in our argument, which indicates write and the plus sign that means it will create a file if it does not exist in library
- The available option beside "w" are "r" for read and "a" for append and plus sign means if it is not there then create it
for i in range(10): f.write("This is line %d\r\n" % (i+1))
- We have a for loop that runs over a range of 10 numbers.
- Using the write function to enter data into the file.
- The output we want to iterate in the file is "this is line number", which we declare with write function and then percent d (displays integer)
- So basically we are putting in the line number that we are writing, then putting it in a carriage return and a new line character
- This will close the instance of the file guru99.txt stored
Here is the result after code execution
When you click on your text file in our case "guru99.txt" it will look something like this
You can also append a new text to the already existing file or the new file.
Once again if you could see a plus sign in the code, it indicates that it will create a new file if it does not exist. But in our case we already have the file, so we are not required to create a new file.
for i in range(2): f.write("Appended line %d\r\n" % (i+1))
This will write data into the file in append mode.
You can see the output in "guru99.txt" file. The output of the code is that line number 1 is appended with line number 2.
Not only you can create .txt file from Python but you can also call .txt file in a "read mode"(r).
Step 1) Open the file in Read mode
Step 2) We use the mode function in the code to check that the file is in open mode. If yes, we proceed ahead
if f.mode == 'r':
Step 3) Use f.read to read file data and store it in variable content
Step 4) print contents
Here is the output
Step 5) You can also read your .txt file line by line if your data is too big to read. This code will segregate your data in easy to ready mode
When you run the code (f1=f.readlines()) for reading the file or document line by line, it will separate each line and present the file in a readable format. In our case the line is short and readable, the output will look similar to the read mode. But if there is a complex data file which is not readable, this piece of code could be useful.
Here is the complete code
def main(): f= open("guru99.txt","w+") #f=open("guru99.txt","a+") for i in range(10): f.write("This is line %d\r\n" % (i+1)) f.close() #Open the file back and read the contents #f=open("guru99.txt", "r") # if f.mode == 'r': # contents =f.read() # print contents #or, readlines reads the individual line into a list #fl =f.readlines() #for x in fl: #print x if __name__== "__main__": main()
- Python allows you to read, write and delete files
- Use the function open("filename","w+") to create a file. The + tells the python compiler to create a file if it does not exist
- To append data to an existing file use the command open("Filename", "a")
- Use the read function to read the ENTIRE contents of a file
- Use the readlines function to read the content of the file one by one.