Python Files Handling - ProgrammerTech
Language:

Search

Python Files Handling

  • Share this:
Python Files Handling

File processing

Files Handling is intended to perform an operation on files of all kinds (such as txt - jpg - mp4). In this lesson, you will learn how to read content from a file, how to create a copy of it, how to modify its content, how to delete it, etc... For example, we have a txt file on the desktop with data inside and we want to read it or write it.

 

open() function

The open() function is one of the ready-made functions in Python and it is used to create a new file or open this file that will be dealt with, and if the file was created correctly or the file was opened, it returns a file object that allows it to be handled, and if it is not You cannot create or access the file. It throws an exception.

The general form of building an open() function in Python

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None)

whereas:

  1. In place of the parameter file we pass a text representing the name of the file that will be created or handled.
  2. mode Optional parameter, we pass in its place one or more characters and it represents how we will deal with the file, whether reading from it or writing in it.
  3. buffering An optional parameter in which we can pass a number that specifies how characters will be cached while writing or reading from this file.
  4. encoding is an optional parameter and we can pass in its place the name of the encoding that we should use when we work with the file.
  5. errors is an optional parameter and we can pass a word in its place in order to specify how errors that may occur when dealing with this file will be handled.
  6. newline is also an optional parameter, we can pass in its place the symbol that represents the end of each line in the file and make the text that is placed some of it down on a new line.

The most important parameter in this function is the mode parameter, because we said that the letter we pass in its place determines the purpose of opening the file.

 

Symbols used with files in Python

Below we will list some of the symbols that we use with files in Python.

  • The symbol "r" is for reading.
  • The symbol "w" is for writing.
  • The symbol "a" is for addition.
  • The symbol "r+" is read and added.
  • The symbol "w+" deletes the previous file and adds a new one with the same name.
  • The symbol "a+" adds the specified file with its name if it does not exist.
  • The symbol "rb and rb+" reads the bytes of an image.
  • The symbol "ab and ab+" adds more bytes from a file and merges them together.
  • The symbol "wb and wb+" reads and writes the bytes of an image.

Example

Let's create a txt file on the desktop and name the file for example 1 and write Programmer Tech in it. We will read the file using Python, the first thing we do is specify the path of this file via file_dir.

>>>file_dir = "C:\\Users\\sama\\Desktop\\1.txt"

For example, let's create a variable called fp

>>>file_dir = "C:\\Users\\sama\\Desktop\\1.txt"
>>>fp = open (file_dir, "r")
>>>fp.read()
'programmer tech'

As we noticed in the previous example, first we specify the file path, then create a variable and read what's inside the file.

Let's take another example and add several lines to the file named 1.

>>>file_dir = "C:\\Users\\sama\\Desktop\\1.txt"
>>>fp = open(file_dir,"r")
>>>print (fp.readlines())

The result will be as an array form:

['programmer tech\n', 'database\n', 'ahmedkaissar']

We used the readlines function in the previous example. But if we want to print the first line, we just write its index as follows

print (fp.readlines() [0] )

And to write something within the data we use the letter a as follows

file_dir = "C:\\Users\\sama\\Desktop\\1.txt"
fp = open(file_dir,"a")
fp.write("hello")
fp.close

And to remove all data from the file and write an alternative we use the letter w.

fp = open(file_dir,"w")

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com