Sitemap
İclal Şeyma Koca

İclal Şeyma Koca

Mechanical Engineer

File Operations Python

--

Press enter or click to view image in full size

Hello, I am Şeyma, one of the participants of the SistersLab Women in Tech Academy project supported by Toplum Gonulluleri Vakfı (https://www.tog.org.tr/en/).

The project aims to empower people between the age of 20–28 who identify as women, through 3 months long software training and supporting training to increase their participation in the workforce in the IT sector.

You can access detailed information about the project using this link: https://sisterslab.co/women-in-tech-academy/

I will share with you what I learned about python as part of this beautiful project. I wish you good reading.

In this story, I will talk about file operations, I wish you a good read.

The first thing to mention for file operations is opening, closing, and reading operations.

After typing open, we can open the files by putting a comma between the file path and the access mode in two parentheses and typing it in double quotes. The letter w opens files for writing purposes. If a file already exists, it overwrites the existing file.

The letter a will open the file in append mode. And adds a newline to the end of the file.

The answer to the question of how to read a file is to use the file path and the letter r for the opened file. But it will give an error when we try to read a file that does not exist. Using the file path and the letter r in two parentheses after typing open is an action for reading only.

Get İclal Şeyma Koca’s stories in your inbox

Join Medium for free to get updates from this writer.

Of course, we can check if the file we want to read exists. we can query the existence of the file with the help of the word that exists by simply searching among the files.

File Reading Methods

We use readlines() to read line by line. This keyword will also read the file as a list.

Adding to a File Opened in Reading Mode

After typing the open parantes file path, it is possible to add rt instead of the letter r and use the f.write pattern.

File Writing Methods

To write a file, it should use the letters w, w+, a, and a+ and the write() and writelines() patterns.

Searching for a File

The file.seek pattern is used to search within the file. It is necessary to write the position of the bookmark in the parenthesis. this will take you directly to the location you want.

Renaming Files

You can do this by using the os.rename pattern and then opening the parentheses. It will be sufficient to write the name of the file you want to change first, followed by a comma, and then the new file name you want in the second bracket.

Delete File

The file is deleted with the os.remove the pattern. If you use it as os.remove(the file name you want to delete), the file you want will be deleted. but of course, it is not possible to delete a file that does not exist.

You have reached the end of this story. I hope it was informative for you, see you in the next story.

Press enter or click to view image in full size

--

--