

This can be used when the file that you are trying to open is in the same directory or folder as the Python script, like this:īut if the file is within a nested folder, like this: The names.txt file is in the "data" folder We usually use a relative path, which indicates where the file is located relative to the location of the script (Python file) that is calling the open() function.įor example, the path in this function call: open("names.txt") # The relative path is "names.txt" The first parameter of the open() function is file, the absolute or relative path to the file that you are trying to work with. To learn more about them, please read this article in the documentation. There are six additional optional arguments. 💡 Tip: These are the two most commonly used arguments to call this function. One of the most important functions that you will need to use as you work with files in Python is open(), a built-in function that opens a file and allows your program to use it and work with it. Let's begin! ✨ 🔹 Working with Files: Basic Syntax How to handle exceptions that could be raised when you work with files.How to work with context managers and why they are useful.

