Python File Handling Functions


File Handling Functions in Python

File handling in Python involves several built-in functions and methods that allow you to work with files easily. These functions enable you to create, read, write, and manipulate files efficiently. Below are the key file handling functions you need to know, along with their usage and examples.

1. open()

The open() function is used to open a file and returns a file object. You can specify the file name and the mode in which to open the file.

Syntax

file_object = open(file_name, mode)
  • Parameters:
    • file_name: The name of the file (can include the path).
    • mode: The mode in which to open the file (e.g., 'r', 'w', 'a', 'b', etc.).

Example

# Opening a file in read mode file = open('example.txt', 'r')

2. close()

The close() method is used to close an opened file. It’s important to close files to free up system resources.

Syntax

file_object.close()

Example

file = open('example.txt', 'r') # Perform file operations file.close() # Closing the file

3. read()

The read() method reads the entire content of the file. You can also specify the number of bytes to read.

Syntax

content = file_object.read(size)
  • Parameters:
    • size (optional): The number of bytes to read. If omitted, the entire file is read.

Example

file = open('example.txt', 'r') content = file.read() # Read the entire file print(content) file.close()

4. readline()

The readline() method reads a single line from the file each time it is called.

Syntax

line = file_object.readline()

Example

file = open('example.txt', 'r') line = file.readline() # Read the first line print(line) file.close()

5. readlines()

The readlines() method reads all the lines in a file and returns them as a list of strings.

Syntax

lines = file_object.readlines()

Example

file = open('example.txt', 'r') lines = file.readlines() # Read all lines for line in lines: print(line.strip()) # Print each line without leading/trailing whitespace file.close()

6. write()

The write() method is used to write a string to a file. If the file is opened in write mode ('w'), it will overwrite the existing content.

Syntax

file_object.write(string)

Example

file = open('example.txt', 'w') file.write('Hello, World!\n') # Write a string to the file file.close()

7. writelines()

The writelines() method is used to write a list of strings to a file.

Syntax

file_object.writelines(list_of_strings)

Example

lines = ['Line 1\n', 'Line 2\n', 'Line 3\n'] file = open('example.txt', 'w') file.writelines(lines) # Write multiple lines to the file file.close()

8. flush()

The flush() method is used to flush the internal buffer of the file. This is useful when you want to ensure that data is written to the file immediately.

Syntax

file_object.flush()

Example

file = open('example.txt', 'w') file.write('Immediate write\n') file.flush() # Ensure data is written to the file file.close()

9. seek()

The seek() method is used to change the current file position. You can move to a specific byte position in the file.

Syntax

file_object.seek(offset, whence)
  • Parameters:
    • offset: The number of bytes to move the cursor.
    • whence: The reference point for the offset (e.g., 0 for the beginning, 1 for the current position, 2 for the end).

Example

file = open('example.txt', 'r') file.seek(0) # Move to the beginning of the file print(file.read()) # Read from the beginning file.close()

10. tell()

The tell() method returns the current position of the file cursor.

Syntax

position = file_object.tell()

Example

file = open('example.txt', 'r') file.read(10) # Read the first 10 bytes position = file.tell() # Get the current position print(position) # Output will be 10 file.close()

Summary

  • Opening a File: Use open() to open a file.
  • Closing a File: Use close() to close an opened file.
  • Reading: Use read(), readline(), or readlines() to read from a file.
  • Writing: Use write() and writelines() to write to a file.
  • Buffer Management: Use flush() to flush the buffer.
  • Position Management: Use seek() to move the cursor and tell() to check its position.

These functions and methods provide a comprehensive way to handle files in Python, allowing you to perform various operations effectively!