Working With Files In Python

Python developers have added many built-in functions to deal with files and make it very easy to open and add data to files compared to other programming languages, which are problematic when working with files. This article will help you learn some simple commands to interact with files and add some data to a new file.

1. Opening The File

Working with files in python is so easy to do. You need to know the file’s name, directory, and done. Let’s see an example of this:

# Open a File
file = open("Example.txt", "r")

# Print The File Information
print(file)

You will get some information about this file, like its name (We already know it) and the encoding type, which is UTF-8 in this case. 

But in the above example, we were interested in printing the data or text inside that file when we’ve used the print() function. To print the file text, we use the for loop:

# Open a File
file = open("Example.txt", "r")

# Print File Data
for line in file:
    print (line)

This code will give you the text inside the file. You can create a simple .txt file and put some text on it to try this example. The for loop will over the text line by line and print them all for you.

There are many ways to read the file, not just by using the for loops. Let’s see another way of performing the same action:

# Open a File
file = open("Example.txt", "r")

# Print File Data
print (file.read())

We use the .read() function to read the content of the whole file instead of by creating a for loop to loop over the whole file and print these lines of text. Let’s see another way of performing the same action using the “with” statement:

# Open a File
with open('Example.txt', 'r') as f:
    # Read The File
    data = f.read()
    # Print File Data
    print(data)

We store the file as an object in the f word (Choose any word you want), and we use the .read() function to read the data and print it later with the print() function.

2. The Open File Mode

You can notice that we’ve used the character “r” as the last argument in the open() function. This is called the mode, which tells python the first time when accessing the file what kind of operations we will perform. The “r” character means reading the file, but many other modes are used. Let’s see these types of modes:

  • r: means opening the file in the read mode
  • w: open the file for adding some data to this file. Notice that if you have some data in this file, it will be overridden.
  • a: it will also write to an existing file, but the good thing is that it won’t override the data inside the file.
  • r+: If you want to read & write the file, then use this mode, and it won’t override the previous data inside the file
  • w+: this mode is the same as the previous one since it will write & read the data, but the previous data will be overridden.
  • a+: it will append the data and read it. It won’t remove the existing data

We will use some of them in this article. Continue reading the rest of it.

3. Create a File

Creating a file and adding some data inside this file is very simple. You need just a few commands, and it is done:

# Create a File In The Write Mode
file = open("Capitals.txt", "w")

# Write To The File
file.write("London ")
file.write("Berlin")

# Close The File
file.close()

# Open The File
file = open("Capitals.txt", "r")

# Print The Data
print (file.read())

First, we use the open() function to create a file called “Capitals.txt” in the write mode. As you can see, we’ve specified the “w” character as the mode. 

We use the .write() function for writing the data inside the file. Finally, it is mandatory to close the file after writing the data inside it, and we use the .close() function. It will close all the resources used to add the data to the file and free your operating system.

We’ve repeated the same code for opening and reading the data from the file to make sure that the writing code is working correctly and you can see the exact words you wrote inside the file on your screen.

4. Adding Data Using The append Mode

We’ve talked that some modes will override the data inside the file, which is not helpful in many cases, but you can add the data without removing the already existing one. Let’s see an example:

# Create a File In The Write Mode
file = open("Capitals.txt", "a")

# Write To The File
file.write(" Paris ")
file.write("Roma")

# Close The File
file.close()

# Open The File
file = open("Capitals.txt", "r")

# Print The Data
print (file.read())

We’ve used the same code as the previous example except changing the mode at the first time from “w” to “a” which means open it in the write mode but don’t override the data later after adding some text. 

*You can see that we’ve added two words: “Paris” & “Roma” and if you open the file in the read mode and see its content, you won’t see only the new data but also the previous one.

Conclusion

Thanks for reading! The article has introduced these simple ways of interacting with the files and how you can add some simple data without even overriding the previous one and more. There is a lot to cover about this topic, but it was an introduction about how easy working with files in python.

Scroll to Top