Wednesday, August 17, 2022

How to open a file in Python?

To open a file you have to use the built-in open function. The general form of opening a file is:
variable_name = open("file_name.extension")
where open is a built-in Python function which is used to open files. The argument in the brackets ("file_name.extension") is the name of the file you want to open with file extension (e.g. txt, data,...).
Note:If you are opening the file which is located at the same directory as a Python script than you type file_name.extension. However, if you are trying to open a file that is located in a different folder then you have to type the address and at the end of the address you have to type the file_name.extension. So the general form of opening a file that is not located in the same folder as the Python script can be written as:
file_name = open("C://Working folder//file_name.extension")
In this general case the "C:" was the most common name of the hard drive on which the working folder with the file_name.extension is located that you are trying to open. However, if the file_name.extension is located at another working_folder on some other hard drive then your address must start with the drive letter at which your file is located.
Example 1 Create .txt file using Notepad or any other word processing program. Write something to this file, then save it under the name "MyFirstNote.txt" and open it with Python script. You should save the file in the same directory as the Python script.
Solution: Create MyFirstNote.txt using Notepad and save it in the same folder where the current Python script is saved. In MyFirstNote.txt we will write a string: "This is my first file that will be opened using Python.". To open the file in Python in Python script write the following code.
file = open("MyFirstNote.txt")
This code line will open the file "MyFirstNote.txt" and assign the file to the variable named file. Let's see what will happen when we use the print function on variable file.
print("file = {}".format(file))
The entire code used in this example is written below.
file = open("MyFirstNote.txt")
print("file = {}".format(file))
The output is given below.
file=< _io.TextIOWrapper name='MyFirstNote.txt' mode='r' encoding='cp1252'>
From the obtained output it can be noticed that the opening of the file was successful since the system return a file handle. As you can see the file handle did not show the data contained in a file, but instead a "handle" that is used to read the data. So if the requested file exists (in this case it does) you are given a handle and you have the permission to read the file. In the next example, we will see what Python returns if the file does not exist.
Example 2 Without creating any new .txt file write in the same Python script used in the previous example procedure for opening the file "MyFile2.txt".
Solution: To open the file "MyFile2.txt" we will use the built-in function "open" and in brackets write the name of the file "MyFile2.txt". The open file will be assigned to variable name file2.
file2 = open("MyFile2.txt")
After running the code Python will retrieve the FileNotFoundError as shown below.
FileNotFoundError: [Errno 2] No such file or directory: 'MyFile2.txt'

No comments:

Post a Comment