Playing with files in Python

Files are important. Even if we don’t think about working with file, we do it. Everyone who starts programming needs to know the basics and how to work with files.

In this article, I would like to show you how easy you can handle files in Python.

1. What can you do with files?

First thing you think is creating the file. Once your file has been created, you can save your information here. Then, you think about opening and closing them. Also, you can delete them.

Tidbit: you can also concatenate files. What does it mean? You have multiple files and you create one file from them.

2. Creating a file

Bellow, you can see how the command looks like. File=Open(here you have to specify the path the the file, ‘file parameter’).

You can specify several file parameters . For example “w” means that the file can be edited and read. “r” means that file can be opened in read only mode. If you use “a” that whatever you save to the file will be appended in it. What does it mean? When you use the “w” parameter than you overwrite the file content and for “a” you just append the new lines to the file.†

If you type this, it’ll look like on video.

Now we have an empty file. Time to save some data in it!

3. Save data in a file

Once the file is created you can put some content into it. This is a very simple operation as there is a write function for this purpose

The number 23 is not a magical number – this is a number of characters that I have written to the file.

Remember that you need to close the file to make sure the content has been saved!

I have opened the file in the Notepad and the content is as follow:

4. Open a file

Imaine you need to write something to a file once the file is on the disk already and have some data. In the orevious exapmple I have closed the file at the end. If you try to write the data but the file is not opened you end up with error:

That means you need to open the file so it can be edited. I will show you now what happens if I open the file for write (means I use the ‘w’ parameter)

Now open the file and you see that the line I put there in the first example has disapeared. The ‘w’ parameter means the file is opened as it was empty.

Sometimes it is a good option but what should you do if you need to add something to the existing file and not to erase the previous content? Use the ‘a’ parameter when you open the file! Try that out:

Are you curious why I have used the \n when writing the data? Open the file so you can see why:

The \n is a “new line” command. It is useful as you can see. But you should try not to use it and then find out that all the data is in one line.

4. Read data from files

Once the file is stored on a disk you can read data from it. There is a ‘r’ file parameter that is used for this purpose. Then use the read function as I show here:

The content variable has all the data from the file. You can display it on the screen with the print function:

Bonus – concatenate files

Sometimes you have many files and need to make one bigger file from them. This operation is called “file concatenation”. I have found a useful code which I present here. First I have created three files with some content. Then I used the code to create another file:

My output.txt file looks like this now:

How do you like it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.