Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 2.42 KB

File metadata and controls

81 lines (61 loc) · 2.42 KB

📁 File Handling

In this course, you will learn about handling files in your programming projects. 📂

Opening and Closing Files

To work with a file in Python, you first need to open it using the open() function. This function takes two arguments: the path to the file and the mode in which you want to open the file. Once you are done with the file, you should close it using the close() method to free up system resources.

Here's an example of opening and closing a file:

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

# do something with the file

# closing the file
file.close()

Reading and Writing Files

After opening a file, you can read or write to it depending on the mode you chose. For example, if you opened the file in read mode, you can only read from it using the read() or readline() methods. On the other hand, if you opened the file in write mode, you can write to it using the write() or writelines() methods.

Here's an example of reading from a file:

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

# reading the contents of the file
content = file.read()

# closing the file
file.close()

print(content)

And here's an example of writing to a file:

# opening a file in write mode
file = open('example.txt', 'w')

# writing to the file
file.write('Hello, world!')

# closing the file
file.close()

File Modes

When opening a file, you can choose from different modes depending on what you want to do with the file. Here are the most common file modes: r: read mode w: write mode a: append mode x: exclusive creation mode b: binary mode t: text mode

Here's an example of opening a file in append mode:

# opening a file in append mode
file = open('example.txt', 'a')

# appending to the file
file.write('\nGoodbye, world!')

# closing the file
file.close()

Error Handling

When working with files, it's important to handle errors that may occur. For example, if the file you're trying to open doesn't exist, you'll get a FileNotFoundError exception. To handle exceptions, you can use a try-except block.

Here's an example of error handling when opening a file:

try:
    # opening a file that may not exist
    file = open('example.txt', 'r')
except FileNotFoundError:
    # handling the exception
    print('The file does not exist.')