Blog

  • File Methods

    A file object is created using open() function. The file class defines the following methods with which different file IO operations can be done. The methods can be used with any file like object such as byte stream or network stream.

    Sr.No.Methods & Description
    1file.close()Close the file. A closed file cannot be read or written any more.
    2file.flush()Flush the internal buffer, like stdio’s fflush. This may be a no-op on some file-like objects.
    3file.fileno()Returns the integer file descriptor that is used by the underlying implementation to request I/O operations from the operating system.
    4file.isatty()Returns True if the file is connected to a tty(-like) device, else False.
    5file.next()Returns the next line from the file each time it is being called.
    6file.read([size])Reads at most size bytes from the file (less if the read hits EOF before obtaining size bytes).
    7file.readline([size])Reads one entire line from the file. A trailing newline character is kept in the string.
    8file.readlines([sizehint])Reads until EOF using readline() and return a list containing the lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.
    9file.seek(offset[, whence])Sets the file’s current position
    10file.tell()Returns the file’s current position
    11file.truncate([size])Truncates the file’s size. If the optional size argument is present, the file is truncated to (at most) that size.
    12file.write(str)Writes a string to the file. There is no return value.
    13file.writelines(sequence)Writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings.
  • Directories

    Directories in Python

    In Python, directories, commonly known as folders in operating systems, are locations on the filesystem used to store files and other directories. They serve as a way to group and manage files hierarchically.

    Python provides several modules, primarily os and os.path, along with shutil, that allows you to perform various operations on directories.

    These operations include creating new directories, navigating through existing directories, listing directory contents, changing the current working directory, and removing directories.

    Checking if a Directory Exists

    Before performing operations on a directory, you often need to check if it exists. We can check if a directory exists or not using the os.path.exists() function in Python.

    This function accepts a single argument, which is a string representing a path in the filesystem. This argument can be −

    • Relative path − A path relative to the current working directory.
    • Absolute path − A complete path starting from the root directory.

    Example

    In this example, we check whether the given directory path exists using the os.path.exists() function −

    import os
    
    directory_path ="D:\\Test\\MyFolder\\"if os.path.exists(directory_path):print(f"The directory '{directory_path}' exists.")else:print(f"The directory '{directory_path}' does not exist.")

    Following is the output of the above code −

    The directory 'D:\\Test\\MyFolder\\' exists.
    

    Creating a Directory

    You create a new directory in Python using the os.makedirs() function. This function creates intermediate directories if they do not exist.

    The os.makedirs() function accepts a “path” you want to create as an argument. It optionally accepts a “mode” argument that specifies the permissions o set for the newly created directories. It is an integer, represented in octal format (e.g., 0o755). If not specified, the default permissions are used based on your system’s umask.

    Example

    In the following example, we are creating a new directory using the os.makedirs() function −

    import os
    
    new_directory ="new_dir.txt"try:
       os.makedirs(new_directory)print(f"Directory '{new_directory}' created successfully.")except OSError as e:print(f"Error: Failed to create directory '{new_directory}'. {e}")

    After executing the above code, we get the following output −

    Directory 'new_dir.txt' created successfully.
    

    The mkdir() Method

    You can use the mkdir() method of the os module to create directories in the current directory. You need to supply an argument to this method, which contains the name of the directory to be created.

    Following is the syntax of the mkdir() method in Python −

    os.mkdir("newdir")

    Example

    Following is an example to create a directory test in the current directory −

    import os
    
    # Create a directory "test"
    os.mkdir("test")print("Directory created successfully")

    The result obtained is as shown below −

    Directory created successfully
    

    Get Current Working Directory

    To retrieve the current working directory in Python, you can use the os.getcwd()function. This function returns a string representing the current working directory where the Python script is executing.

    Syntax

    Following is the basic syntax of the getcwd() function in Python −

    os.getcwd()

    Example

    Following is an example to display the current working directory using the getcwd() function −

    import os
    
    current_directory = os.getcwd()print(f"Current working directory: {current_directory}")

    We get the output as follows −

    Current working directory: /home/cg/root/667ba7570a5b7
    

    Listing Files and Directories

    You can list the contents of a directory using the os.listdir() function. This function returns a list of all files and directories within the specified directory path.

    Example

    In the example below, we are listing the contents of the specified directory path using the listdir() function −

    import os
    
    directory_path =r"D:\MyFolder\Pictures"try:
       contents = os.listdir(directory_path)print(f"Contents of '{directory_path}':")for item in contents:print(item)except OSError as e:print(f"Error: Failed to list contents of directory '{directory_path}'. {e}")

    Output of the above code is as shown below −

    Contents of 'D:\MyFolder\Pictures':
    Camera Roll
    desktop.ini
    Saved Pictures
    Screenshots
    

    Changing the Current Working Directory

    You can change the current directory using the chdir() method. This method takes an argument, which is the name of the directory that you want to make the current directory.

    Syntax

    Following is the syntax of the chdir() method in Python −

    os.chdir("newdir")

    Example

    Following is an example to change the current directory to Desktop using the chdir() method −

    import os
    
    new_directory =r"D:\MyFolder\Pictures"try:
    
    os.chdir(new_directory)print(f"Current working directory changed to '{new_directory}'.")except OSError as e:print(f"Error: Failed to change working directory to '{new_directory}'. {e}")</pre>

    We get the output as shown below −

    Current working directory changed to 'D:\MyFolder\Pictures'.
    

    Removing a Directory

    You can remove an empty directory in Python using the os.rmdir() method. If the directory contains files or other directories, you can use shutil.rmtree()method to delete it recursively.

    Syntax

    Following is the basic syntax to delete a directory in Python −

    os.rmdir(directory_path)# or
    shutil.rmtree(directory_path)

    Example

    In the following example, we remove an empty directory using the os.rmdir() method −

    import os
    directory_path =r"D:\MyFolder\new_dir"try:
       os.rmdir(directory_path)print(f"Directory '{directory_path}' successfully removed.")except OSError as e:print(f"Error: Failed to remove directory '{directory_path}'. {e}")

    It will produce the following output −

    Directory 'D:\MyFolder\new_dir' successfully removed.
  •  Renaming and Deleting Files

    Renaming and Deleting Files in Python

    In Python, you can rename and delete files using built-in functions from the osmodule. These operations are important when managing files within a file system. In this tutorial, we will explore how to perform these actions step-by-step.

    Renaming Files in Python

    To rename a file in Python, you can use the os.rename() function. This function takes two arguments: the current filename and the new filename.

    Syntax

    Following is the basic syntax of the rename() function in Python −

    os.rename(current_file_name, new_file_name)

    Parameters

    Following are the parameters accepted by this function −

    • current_file_name − It is the current name of the file you want to rename.
    • new_file_name − It is the new name you want to assign to the file.

    Example

    Following is an example to rename an existing file “oldfile.txt” to “newfile.txt” using the rename() function −

    import os
    
    # Current file name
    current_name ="oldfile.txt"# New file name
    new_name ="newfile.txt"# Rename the file
    os.rename(current_name, new_name)print(f"File '{current_name}' renamed to '{new_name}' successfully.")

    Following is the output of the above code −

    File 'oldfile.txt' renamed to 'newfile.txt' successfully.
    

    Deleting Files in Python

    You can delete a file in Python using the os.remove() function. This function deletes a file specified by its filename.

    Syntax

    Following is the basic syntax of the remove() function in Python −

    os.remove(file_name)

    Parameters

    This function accepts the name of the file as a parameter which needs to be deleted.

    Example

    Following is an example to delete an existing file “file_to_delete.txt” using the remove() function −

    import os
    
    # File to be deleted
    file_to_delete ="file_to_delete.txt"# Delete the file
    os.remove(file_to_delete)print(f"File '{file_to_delete}' deleted successfully.")

    After executing the above code, we get the following output −

    File 'file_to_delete.txt' deleted successfully.
  •  Read Files

    Reading from a file involves opening the file, reading its contents, and then closing the file to free up system resources. Python provides several methods to read from a file, each suited for different use cases.

    Opening a File for Reading

    Opening a file is the first step in reading its contents. In Python, you use the open() function to open a file. This function requires at least one argument, the filename, and optionally a mode that specifies the purpose of opening the file.

    To open a file for reading, you use the mode ‘r’. This is the default mode, so you can omit it if you only need to read from the file.

    Reading a File Using read() Method

    The read() method is used to read the contents of a file in Python. It reads the entire content of the file as a single string. This method is particularly useful when you need to process the whole file at once.

    Syntax

    Following is the basic syntax of the read() method in Python −

    file_object.read(size)

    Where, 

    • file_object is the file object returned by the open() function.
    • size is the number of bytes to read from the file. This parameter is optional. If omitted or set to a negative value, the method reads until the end of the file.

    Example

    In the following example, we are opening the file “example.txt” in read mode. We then use the read() method to read the entire content of the file −

    # Open the file in read modefile=open('example.txt','r')# Read the entire content of the file
    content =file.read()# Print the contentprint(content)# Close the filefile.close()

    After executing the above code, we get the following output −

    welcome to Tutorialspoint.
    

    Reading a File Using readline() Method

    The readline() method is used to read one line from a file at a time. This method is useful when you need to process a file line by line, especially for large files where reading the entire content at once is not practical.

    Syntax

    Following is the basic syntax of the readline() method in Python −

    file_object.readline(size)

    Where, 

    • file_object is the file object returned by the open() function.
    • size is an optional parameter specifying the maximum number of bytes to read from the line. If omitted or set to a negative value, the method reads until the end of the line.

    Example

    In the example below, we are opening the file “example.txt” in read mode. We then use the readline() method to read the first line of the file −

    # Open the file in read modefile=open('example.txt','r')# Read the first line of the file
    line =file.readline()# Print the lineprint(line)# Close the filefile.close()

    Following is the output of the above code −

    welcome to Tutorialspoint.
    

    Reading a File Using readlines() Method

    The readlines() method reads all the lines from a file and returns them as a list of strings. Each string in the list represents a single line from the file, including the newline character at the end of each line.

    This method is particularly useful when you need to process or analyse all lines of a file at once.

    Syntax

    Following is the basic syntax of the readlines() method in Python −

    file_object.readlines(hint)

    Where, 

    • file_object is the file object returned by the open() function.
    • hint is an optional parameter that specifies the number of bytes to read. If specified, it reads lines up to the specified bytes, not necessarily reading the entire file.

    Example

    In this example, we are opening the file “example.txt” in read mode. We then use the readlines() method to read all the lines from the file and return them as a list of strings −

    # Open the file in read modefile=open('example.txt','r')# Read all lines from the file
    lines =file.readlines()# Print the linesfor line in lines:print(line, end='')# Close the filefile.close()

    Output of the above code is as shown below −

    welcome to Tutorialspoint.
    Hi Surya.
    How are you?.
    

    Using “with” Statement

    The “with” statement in Python is used for exception handling. When dealing with files, using the “with” statement ensures that the file is properly closed after reading, even if an exception occurs.

    When you use a with statement to open a file, the file is automatically closed at the end of the block, even if an error occurs within the block.

    Example

    Following is a simple example of using the with statement to open, read, and print the contents of a file −

    # Using the with statement to open a filewithopen('example.txt','r')asfile:
       content =file.read()print(content)

    We get the output as follows −

    welcome to Tutorialspoint.
    Hi Surya.
    How are you?.
    

    Reading a File in Binary Mode

    By default, read/write operation on a file object are performed on text string data. If we want to handle files of different types, such as media files (mp3), executables (exe), or pictures (jpg), we must open the file in binary mode by adding the ‘b’ prefix to the read/write mode.

    Writing to a Binary File

    Assuming that the test.bin file has already been written in binary mode −

    # Open the file in binary write modewithopen('test.bin','wb')as f:
       data =b"Hello World"
       f.write(data)

    Example

    To read a binary file, we need to open it in ‘rb’ mode. The returned value of the read() method is then decoded before printing −

    # Open the file in binary read modewithopen('test.bin','rb')as f:
       data = f.read()print(data.decode(encoding='utf-8'))

    It will produce the following output −

    Hello World
    

    Reading Integer Data From a File

    To write integer data to a binary file, the integer object should be converted to bytes using the to_bytes() method.

    Writing an Integer to a Binary File

    Following is an example on how to write an integer to a binary file −

    # Convert the integer to bytes and write to a binary file
    n =25
    data = n.to_bytes(8,'big')withopen('test.bin','wb')as f:
       f.write(data)

    Reading an Integer from a Binary File

    To read back the integer data from the binary file, convert the output of the read() function back to an integer using the from_bytes() method −

    # Read the binary data from the file and convert it back to an integerwithopen('test.bin','rb')as f:
       data = f.read()
       n =int.from_bytes(data,'big')print(n)

    Reading Float Data From a File

    For handling floating-point data in binary files, we need to use the struct module from Python’s standard library. This module helps convert between Python values and C structs represented as Python bytes objects.

    Writing a Float to a Binary File

    To write floating-point data to a binary file, we use the struct.pack() method to convert the float into a bytes object −

    import struct
    
    # Define a floating-point number
    x =23.50# Pack the float into a binary format
    data = struct.pack('f', x)# Open the file in binary write mode and write the packed datawithopen('test.bin','wb')as f:
       f.write(data)

    Reading Float Numbers from a Binary File

    To read floating-point data from a binary file, we use the struct.unpack() method to convert the bytes object back into a float −

    import struct
    
    # Open the file in binary read modewithopen('test.bin','rb')as f:# Read the binary data from the file
       data = f.read()# Unpack the binary data to retrieve the float
       x = struct.unpack('f', data)[0]# Print the float valueprint(x)

    Reading and Writing to a File Using “r+” Mode

    When a file is opened for reading (with ‘r’ or ‘rb’), writing data is not possible unless the file is closed and reopened in a different mode. To perform both read and write operations simultaneously, we add the ‘+’ character to the mode parameter. Using ‘w+’ or ‘r+’ mode enables using both write() and read() methods without closing the file.

    The File object also supports the seek() function, which allows repositioning the read/write pointer to any desired byte position within the file.

    Syntax

    Following is the syntax for seek() method −

    fileObject.seek(offset[, whence])

    Parameters

    • offset − This is the position of the read/write pointer within the file.
    • whence − This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.

    Example

    The following program opens a file in ‘r+’ mode (read-write mode), seeks a certain position in the file, and reads data from that position −

    # Open the file in read-write modewithopen("foo.txt","r+")as fo:# Move the read/write pointer to the 10th byte position
       fo.seek(10,0)# Read 3 bytes from the current position
       data = fo.read(3)# Print the read dataprint(data)

    After executing the above code, we get the following output −

    rat
    

    Reading and Writing to a File Simultaneously in Python

    When a file is opened for writing (with ‘w’ or ‘a’), it is not possible to read from it, and attempting to do so will throw an UnsupportedOperation error.

    Similarly, when a file is opened for reading (with ‘r’ or ‘rb’), writing to it is not allowed. To switch between reading and writing, you would typically need to close the file and reopen it in the desired mode.

    To perform both read and write operations simultaneously, you can add the ‘+’ character to the mode parameter. Using ‘w+’ or ‘r+’ mode enables both write() and read() methods without needing to close the file.

    Additionally, the File object supports the seek() function, which allows you to reposition the read/write pointer to any desired byte position within the file.

    Example

    In this example, we open the file in ‘r+’ mode and write data to the file. The seek(0) method repositions the pointer to the beginning of the file −

    # Open the file in read-write modewithopen("foo.txt","r+")as fo:# Write data to the file
       fo.write("This is a rat race")# Rewind the pointer to the beginning of the file
       fo.seek(0)# Read data from the file
       data = fo.read()print(data)

    Reading a File from Specific Offset

    We can set the he file’s current position at the specified offset using the seek()method.

    • If the file is opened for appending using either ‘a’ or ‘a+’, any seek() operations will be undone at the next write.
    • If the file is opened only for writing in append mode using ‘a’, this method is essentially a no-op, but it remains useful for files opened in append mode with reading enabled (mode ‘a+’).
    • If the file is opened in text mode using ‘t’, only offsets returned by tell() are legal. Use of other offsets causes undefined behavior.

    Note that not all file objects are seekable.

    Example

    The following example demonstrates how to use the seek() method to perform simultaneous read/write operations on a file. The file is opened in w+ mode (read-write mode), some data is added, and then the file is read and modified at a specific position −

    # Open a file in read-write mode
    fo =open("foo.txt","w+")# Write initial data to the file
    fo.write("This is a rat race")# Seek to a specific position in the file
    fo.seek(10,0)# Read a few bytes from the current position
    data = fo.read(3)print("Data read from position 10:", data)# Seek back to the same position
    fo.seek(10,0)# Overwrite the earlier contents with new text
    fo.write("cat")# Rewind to the beginning of the file
    fo.seek(0,0)# Read the entire file content
    data = fo.read()print("Updated file content:", data)# Close the file
    fo.close()

    Following is the output of the above code −

    Data read from position 10: rat
    Updated file content: This is a cat race
  • Write to File

    Writing to a file involves opening the file in a specific mode, writing data to it, and then closing the file to ensure that all data is saved and resources are released. Python provides a built-in function open() to handle file operations and various methods for writing data.

    Opening a File for Writing

    Opening a file for writing is the first step in performing write operations in Python. The open() function is used to open files in different modes, each suited for specific use cases.

    The open() Function

    The open() function in Python is used to open a file. It requires at least one argument, the name of the file, and can take an optional second argument that specifies the mode in which the file should be opened.

    File Modes for Writing

    Following are the main modes you can use to open a file for writing −

    • w (Write mode) − Opens the file for writing. If the file exists, it truncates (empties) the file before writing. If the file does not exist, it creates a new file.
    • a (Append Mode) − Data is written at the end of the file. If the file does not exist, it creates a new file.
    • x (Exclusive Creation Mode) − Opens the file for exclusive creation. If the file already exists, the operation fails.
    • b (Binary Mode) − When used with other modes, opens the file in binary mode.
    • + (Update Mode) − Opens the file for updating (reading and writing).

    Example: Opening a File in Write Mode

    This mode is used when you want to write data to a file, starting from scratch each time the file is opened −

    file=open("example.txt","w")file.write("Hello, World!")file.close()print("File opened successfully!!")

    Following is the output obtained −

    File opened successfully!!
    

    Example: Opening a File in Append Mode

    This mode is used when you want to add data to the end of the file without altering its existing contents −

    file=open("example.txt","a")file.write("Appending this line.\n")file.close()print("File opened successfully!!")

    This will produce the following result −

    File opened successfully!!

    Writing to a File Using write() Method

    The write() method is used to write a single string to a file. This makes it suitable for various text-based file operations.

    The write() method takes a single argument: the string that you want to write to the file. It writes the exact content of the string to the file without adding any additional characters, such as newlines.

    Example

    In the following example, we are opening the file “example.txt” in write mode. We then use the write() method to write a string to the file −

    # Open a file in write modewithopen("example.txt","w")asfile:file.write("Hello, World!\n")file.write("This is a new line.\n")print("File opened successfully!!")

    Following is the output of the above code −

    File opened successfully!!
    

    Writing to a File Using writelines() Method

    The writelines() method is used to write a list of strings to a file. Each string in the list is written to the file sequentially without adding any newline characters automatically.

    Example

    In this example, we are creating a list of strings, lines, with each string ending in a newline character. We then open a file “example.txt” in write mode and use the writelines() method to write all the strings in the list to the file in one operation −

    # List of lines to write to the file
    lines =["First line\n","Second line\n","Third line\n"]# Open a file in write modewithopen("example.txt","w")asfile:file.writelines(lines)print("File opened successfully!!")

    The output obtained is as shown below −

    File opened successfully!!
    

    Writing to a New File

    Writing to a new file in Python involves creating a new file (or overwriting an existing one) and writing the desired content to it. Here, we will explain the steps involved in writing to a new file −

    • Open the File − Use the open() function to create or open a file in write mode (“w” or “wb” for binary files).
    • Write Data − Use the write() or writelines() method to write data to the file.
    • Close the File − Ensure the file is properly closed after writing, generally using the “with” statement for automatic handling.

    Example

    In the example below, we create a “foo.txt” file and write given content in that file and finally close that file −

    # Open a file
    fo =open("foo.txt","w")
    fo.write("Python is a great language.\nYeah its great!!\n")# Close opened file
    fo.close()

    If you open this file with any text editor application such as Notepad, it will have the following content −

    Python is a great language.
    Yeah its great!!
    

    Writing to a New File in Binary Mode

    By default, read/write operations on a file object are performed on text string data. If we need to handle files of different types, such as media files (mp3), executables (exe), or pictures (jpg), we must open the file in binary mode by adding the ‘b’ prefix to the read/write mode.

    Writing Binary Data to a File

    To write binary data to a file, open the file in binary write mode (‘wb’). The following example demonstrates this −

    # Open a file in binary write modewithopen('test.bin','wb')as f:# Binary data
       data =b"Hello World"  
       f.write(data)

    Converting Text Strings to Bytes

    Conversion of a text string to bytes can be done using the encode() function. This is useful when you need to write text data as binary data −

    # Open a file in binary write modewithopen('test.bin','wb')as f:# Convert text string to bytes
       data ="Hello World".encode('utf-8')  
       f.write(data)

    Writing to an Existing File

    When an existing file is opened in write mode (‘w’), its previous contents are erased. Opening a file with write permission treats it as a new file. To add data to an existing file without erasing its contents, you should open the file in append mode (‘a’).

    Example

    The following example demonstrates how to open a file in append mode and add new text to it −

    # Open a file in append mode
    fo =open("foo.txt","a")
    text ="TutorialsPoint has a fabulous Python tutorial"
    fo.write(text)# Close opened file
    fo.close()

    If you open this file with any text editor application such as Notepad, it will have the following content −

    Python is a great language.
    Yeah its great!!
    TutorialsPoint has a fabulous Python tutorial
    

    Writing to a File in Reading and Writing Modes

    When a file is opened for writing using ‘w’ or ‘a’, it is not possible to perform write operations at any earlier byte position in the file. The ‘w+’ mode, however, allows both reading and writing operations without closing the file. The seek() function is used to move the read/write pointer to any desired byte position within the file.

    Using the seek() Method

    The seek() method is used to set the position of the read/write pointer within the file. The syntax for the seek() method is as follows −

    fileObject.seek(offset[, whence])

    Where,

    • offset − This is the position of the read/write pointer within the file.
    • whence − This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.

    Example

    The following program demonstrates how to open a file in read-write mode (‘w+’), write some data, seek a specific position, and then overwrite part of the file’s content −

    # Open a file in read-write mode
    fo =open("foo.txt","w+")# Write initial data to the file
    fo.write("This is a rat race")# Move the read/write pointer to the 10th byte
    fo.seek(10,0)# Read 3 bytes from the current position
    data = fo.read(3)# Move the read/write pointer back to the 10th byte
    fo.seek(10,0)# Overwrite the existing content with new text
    fo.write('cat')# Close the file
    fo.close()

    If we open the file in read mode (or seek to the starting position while in ‘w+’ mode) and read the contents, it will show the following −

    This is a cat race
  • File Handling

    File handling in Python involves interacting with files on your computer to read data from them or write data to them. Python provides several built-in functions and methods for creating, opening, reading, writing, and closing files. This tutorial covers the basics of file handling in Python with examples.

    Opening a File in Python

    To perform any file operation, the first step is to open the file. Python’s built-in open() function is used to open files in various modes, such as reading, writing, and appending. The syntax for opening a file in Python is −

    file=open("filename","mode")

    Where, filename is the name of the file to open and mode is the mode in which the file is opened (e.g., ‘r’ for reading, ‘w’ for writing, ‘a’ for appending).

    File Opening Modes

    Following are the file opening modes −

    Sr.No.Modes & Description
    1rOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
    2rbOpens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
    3r+Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
    4rb+Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
    5wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
    6bOpens the file in binary mode
    7tOpens the file in text mode (default)
    8+open file for updating (reading and writing)
    9wbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
    10w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
    11wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
    12aOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
    13abOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
    14a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
    15ab+Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
    16xopen for exclusive creation, failing if the file already exists

    Once a file is opened and you have one file object, you can get various information related to that file.

    Example 1

    In the following example, we are opening a file in different modes −

    # Opening a file in read modefile=open("example.txt","r")# Opening a file in write modefile=open("example.txt","w")# Opening a file in append modefile=open("example.txt","a")# Opening a file in binary read modefile=open("example.txt","rb")

    Example 2

    In here, we are opening a file named “foo.txt” in binary write mode (“wb”), printing its name, whether it’s closed, and its opening mode, and then closing the file −

    # Open a file
    fo =open("foo.txt","wb")print("Name of the file: ", fo.name)print("Closed or not: ", fo.closed)print("Opening mode: ", fo.mode)
    fo.close()

    It will produce the following output −

    Name of the file: foo.txt
    Closed or not: False
    Opening mode: wb
    

    Reading a File in Python

    Reading a file in Python involves opening the file in a mode that allows for reading, and then using various methods to extract the data from the file. Python provides several methods to read data from a file −

    • read() − Reads the entire file.
    • readline() − Reads one line at a time.
    • readlines − Reads all lines into a list.

    To read a file, you need to open it in read mode. The default mode for the open() function is read mode (‘r’), but it’s good practice to specify it explicitly.

    Example: Using read() method

    In the following example, we are using the read() method to read the whole file into a single string −

    withopen("example.txt","r")asfile:
       content =file.read()print(content)

    Following is the output obtained −

    Hello!!!
    Welcome to TutorialsPoint!!!
    

    Example: Using readline() method

    In here, we are using the readline() method to read one line at a time, making it memory efficient for reading large files line by line −

    memory efficient for reading large files line by line −

    withopen("example.txt","r")asfile:
       line =file.readline()while line:print(line, end='')
    
      line =file.readline()</pre>

    Output of the above code is as shown below −

    Hello!!!
    Welcome to TutorialsPoint!!!
    

    Example: Using readlines() method

    Now, we are using the readlines() method to read the entire file and splits it into a list where each element is a line −

    withopen("example.txt","r")asfile:
       lines =file.readlines()for line in lines:print(line, end='')

    We get the output as follows −

    Hello!!!
    Welcome to TutorialsPoint!!!
    

    Writing to a File in Python

    Writing to a file in Python involves opening the file in a mode that allows writing, and then using various methods to add content to the file.

    To write data to a file, use the write() or writelines() methods. When opening a file in write mode ('w'), the file's existing content is erased.

    Example: Using the write() method

    In this example, we are using the write() method to write the string passed to it to the file. If the file is opened in 'w' mode, it will overwrite any existing content. If the file is opened in 'a' mode, it will append the string to the end of the file −

    withopen("foo.txt","w")asfile:file.write("Hello, World!")print("Content added Successfully!!")

    Output of the above code is as follows −

    Content added Successfully!!
    

    Example: Using the writelines() method

    In here, we are using the writelines() method to take a list of strings and writes each string to the file. It is useful for writing multiple lines at once −

    lines =["First line\n","Second line\n","Third line\n"]withopen("example.txt","w")asfile:file.writelines(lines)print("Content added Successfully!!")

    The result obtained is as follows −

    Content added Successfully!!
    

    Closing a File in Python

    We can close a file in Python using the close() method. Closing a file is an essential step in file handling to ensure that all resources used by the file are properly released. It is important to close files after operations are completed to prevent data loss and free up system resources.

    Example

    In this example, we open the file for writing, write data to the file, and then close the file using the close() method −

    file=open("example.txt","w")file.write("This is an example.")file.close()print("File closed successfully!!")

    The output produced is as shown below −

    File closed successfully!!
    

    Using "with" Statement for Automatic File Closing

    The with statement is a best practice in Python for file operations because it ensures that the file is automatically closed when the block of code is exited, even if an exception occurs.

    Example

    In this example, the file is automatically closed at the end of the with block, so there is no need to call close() method explicitly −

    withopen("example.txt","w")asfile:file.write("This is an example using the with statement.")print("File closed successfully!!")

    Following is the output of the above code −

    File closed successfully!!
    

    Handling Exceptions When Closing a File

    When performing file operations, it is important to handle potential exceptions to ensure your program can manage errors gracefully.

    In Python, we use a try-finally block to handle exceptions when closing a file. The "finally" block ensures that the file is closed regardless of whether an error occurs in the try block −

    try:file=open("example.txt","w")file.write("This is an example with exception handling.")finally:file.close()print("File closed successfully!!")

    After executing the above code, we get the following output −

    File closed successfully!!
  • Array Exercises

    Example 1

    Python program to find the largest number in an array −

    import array as arr
    a = arr.array('i',[10,5,15,4,6,20,9])print(a)
    largest = a[0]for i inrange(1,len(a)):if a[i]>largest:
    
      largest=a[i]print("Largest number:", largest)</pre>

    It will produce the following output −

    array('i', [10, 5, 15, 4, 6, 20, 9])
    Largest number: 20
    Example 2
    Python program to store all even numbers from an array in another array −

    import array as arr
    a = arr.array('i', [10,5,15,4,6,20,9])
    print (a)
    b = arr.array('i')
    for i in range(len(a)):
    if a[i]%2 == 0:
    b.append(a[i])
    print ("Even numbers:", b)
    It will produce the following output −

    array('i', [10, 5, 15, 4, 6, 20, 9])
    Even numbers: array('i', [10, 4, 6, 20])
    Example 3
    Python program to find the average of all numbers in a Python array −

    import array as arr
    a = arr.array('i', [10,5,15,4,6,20,9])
    print (a)
    s = 0
    for i in range(len(a)):
    s+=a[i]
    avg = s/len(a)
    print ("Average:", avg)

    # Using sum() function
    avg = sum(a)/len(a)
    print ("Average:", avg)
    It will produce the following output −

    array('i', [10, 5, 15, 4, 6, 20, 9])
    Average: 9.857142857142858
    Average: 9.857142857142858
    Exercise Programs
    Python program find difference between each number in the array and the average of all numbers

    Python program to convert a string in an array

    Python program to split an array in two and store even numbers in one array and odd numbers in the other.

    Python program to perform insertion sort on an array.

    Python program to store the Unicode value of each character in the given array.
  • Array Methods

    The array module in Python offers an efficient object type for representing arrays of basic values like characters, integers, and floating point numbers. Arrays are similar to lists but it stores a collection of homogeneous data elements in order. At the time of creating array’s type is specified using a single character type code.

    Array methods provides various operations on array objects, including appending, extending, and manipulating elements. These methods are used for efficient handling of homogeneous collections of basic data types, making them suitable for tasks requiring compact data storage, such as numerical computations.

    Python Array Class

    The array class defines several methods, including adding and removing elements, obtaining information about the array, manipulating array elements, and converting arrays to and from other data types. Below are categorized methods based on their functionality. Let’s explore and understand the functionality of each method.

    Arrays are created using the array.array(typecode[, initializer]) class, where typecode is a single character that defines the type of elements in the array, and initializer is an optional value used to initialize the array.

    Adding and Removing Elements

    Below methods are used for appending, extending, inserting, and removing elements from arrays −

    Sr.No.Methods with Description
    1append(x)Appends a new item with value x to the end of the array.
    2extend(iterable)Appends items from iterable to the end of the array.
    3insert(i, x)Inserts a new item with value x before position i.
    4pop([i])Removes and returns the item with index i. If i is not specified, removes and returns the last item.
    5remove(x)Removes the first occurrence of x from the array.

    Information and Utility Methods

    These methods are used for obtaining information about arrays and to perform utility operations −

    Sr.No.Methods with Description
    1buffer_info()Returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold the arrays contents.
    2count(x)Returns the number of occurrences of x in the array.
    3index(x[, start[, stop]])Returns the smallest index where x is found in the array. Optional start and stop arguments can specify a sub-range to search.

    Manipulating Array Elements

    Following methods are used for manipulating array elements, such as reversing the array or byteswapping values.

    Sr.No.Methods with Description
    1reverse()Reverses the order of the items in the array.
    2byteswap()“Byteswaps” all items of the array, useful for reading data from a file written on a machine with a different byte order.

    Conversion Methods

    These methods are used to convert arrays to and from bytes, files, lists, and Unicode strings.

    Sr.No.Methods with Description
    1frombytes(buffer)Appends items from the bytes-like object, interpreting its content as an array of machine values.
    2tobytes()Converts the array to a bytes representation.
    3fromfile(f, n)Reads n items from the file object f and appends them to the array.
    4tofile(f)Writes all items to the file object f.
    5fromlist(list)Appends items from the list to the array.
    6tolist()Converts the array to a list with the same items.
    7fromunicode(s)Extends the array with data from the given Unicode string. The array must have type code ‘u’.
    8tounicode()Converts the array to a Unicode string. The array must have type code ‘u’.
  • Join Arrays

    The process of joining two arrays is termed as Merging or concatenating. Python provides multiple ways to merge two arrays such as append() and extend() methods. But, before merging two arrays always ensure that both arrays are of the same data type otherwise program will throw error.

    In Pythonarray is a homogenous collection of Python’s built in data types such as strings, integer or float objects. However, array itself is not a built-in type, instead we need to use the Python’s built-in array module.

    Merge Python Array

    Join two Arrays in Python

    To join arrays in Python, use the following approaches −

    • Using append() method
    • Using + operator
    • Using extend() method

    Using append() Method

    To join two arrays, we can append each item from one array to other using append() method. To perform this operation, run a for loop on the original array, fetch each element and append it to a new array.

    Example: Join Two Arrays by Appending Elements

    Here, we use the append() method to join two arrays.

    import array as arr
    
    # creating two arrays
    a = arr.array('i',[10,5,15,4,6,20,9])
    b = arr.array('i',[2,7,8,11,3,10])# merging both arraysfor i inrange(len(b)):
       a.append(b[i])print(a)

    It will produce the following output −

    array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])
    

    Using + operator

    We can also use + operator to concatenate or merge two arrays. In this approach, we first convert arrays to list objects, then concatenate the lists using the + operator and convert back to get merged array.

    Example: Join Two Arrays by Converting to List Objects

    In this example, we will see how to join two arrays using + operator.

    import array as arr
    a = arr.array('i',[10,5,15,4,6,20,9])
    b = arr.array('i',[2,7,8,11,3,10])
    x = a.tolist()
    y = b.tolist()
    z = x+y
    a = arr.array('i', z)print(a)

    The above code will display the following output −

    array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])
    

    Using extend() Method

    Another approach to concatenate arrays is the use of extend() method from the List class. Similar to above approach, we first convert the array to a list and then call the extend() method to merge the two lists.

    Example: Join Two Arrays using extend() Method

    In the following example, we will use the extend() method to concatenate two arrays in Python.

    import array as arr
    a = arr.array('i',[88,99,77,66,44,22])
    b = arr.array('i',[12,17,18,11,13,10])
    a.extend(b)print(a)

    It will produce the following output −

    array('i', [88, 99, 77, 66, 44, 22, 12, 17, 18, 11, 13, 10])
    
  • Sort Arrays

    Python’s array module defines the array class. An object of array class is similar to the array as present in Java or C/C++. Unlike the built-in Python sequences, array is a homogenous collection of either strings, or integers, or float objects.

    The array class doesn’t have any function/method to give a sorted arrangement of its elements. However, we can achieve it with one of the following approaches −

    • Using a sorting algorithm
    • Using the sort() method from List
    • Using the built-in sorted() function

    Let’s discuss each of these methods in detail.

    Python Array Sorting

    Sort Arrays Using a Sorting Algorithm

    We implement the classical bubble sort algorithm to obtain the sorted array. To do it, we use two nested loops and swap the elements for rearranging in sorted order.

    Example

    Run the following code using a Python code editor −

    import array as arr
    a = arr.array('i',[10,5,15,4,6,20,9])for i inrange(0,len(a)):for j inrange(i+1,len(a)):if(a[i]> a[j]):
    
         temp = a[i];
         a[i]= a[j];
         a[j]= temp;print(a)</pre>

    It will produce the following output −

    array('i', [4, 5, 6, 9, 10, 15, 20])
    Sort Arrays Using sort() Method of List
    Even though array module doesn't have a sort() method, Python's built-in List class does have a sort method. We shall use it in the next example.

    First, declare an array and obtain a list object from it, using tolist() method. Then, use the sort() method to get a sorted list. Lastly, create another array using the sorted list which will display a sorted array.

    Example
    The following code shows how to get sorted array using the sort() method.

    import array as arr

    # creating array
    orgnlArray = arr.array('i', [10,5,15,4,6,20,9])
    print("Original array:", orgnlArray)
    # converting to list
    sortedList = orgnlArray.tolist()
    # sorting the list
    sortedList.sort()

    # creating array from sorted list
    sortedArray = arr.array('i', sortedList)
    print("Array after sorting:",sortedArray)
    The above code will display the following output −

    Original array: array('i', [10, 5, 15, 4, 6, 20, 9])
    Array after sorting: array('i', [4, 5, 6, 9, 10, 15, 20])
    Sort Arrays Using sorted() Method
    The third technique to sort an array is with the sorted() function, which is a built-in function.

    The syntax of sorted() function is as follows −

    sorted(iterable, reverse=False)
    The function returns a new list containing all items from the iterable in ascending order. Set reverse parameter to True to get a descending order of items.

    The sorted() function can be used along with any iterable. Python array is an iterable as it is an indexed collection. Hence, an array can be used as a parameter to sorted() function.

    Example
    In this example, we will see the use of sorted() method in sorting an array.

    import array as arr
    a = arr.array('i', [4, 5, 6, 9, 10, 15, 20])
    sorted(a)
    print(a)
    It will produce the following output −

    array('i', [4, 5, 6, 9, 10, 15, 20])