File Operations
- How to Create / Open File open()
file_name = open(path/file_name, file_mode)
by default file mode is "rt"
mode as "w", 'a" and "x" will create file.
- How to Close File close()
file_name.close()
Example : create file
- program to create file
#program to create file for first time
file = open("my_file.txt", "x")
print(file)
print("file created")
file.close()
#Output
<_io.TextIOWrapper name='my_file.txt' mode='x' encoding='cp1252'>
file created
- program to create same file 2nd time, open() will return File_Exists_Error
#program to create file if already exist
file = open("my_file.txt", "x")
print(file)
print("file created")
file.close()
#Output
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 3, in <module>
file = open("my_file.txt", "x")
FileExistsError: [Errno 17] File exists: 'my_file.txt'
Example : program to open file in read mode
- when file exist
#program to open file in read mode if file exist
file = open("my_file.txt", "r")
print(file)
print("file opened successfully")
file.close()
#Output
<_io.TextIOWrapper name='my_file.txt' mode='r' encoding='cp1252'>
file opened successfully
- when file does not exist, open() will return File_Not_Found_Error
#program to open file in read mode if file exist
file = open("my_file.txt", "r")
print(file)
print("file opened successfully")
file.close()
#Output
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 3, in <module>
file = open("my_file.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'my_file.txt'
Example : program to open file in write mode
- if file not present, it will create new file and open the file.
- if file exist, it will open file and delete all the data from the file.
#program
file = open("my_file.txt", "w")
print(file)
print("file opened successfully")
file.close()
#Output
<_io.TextIOWrapper name='my_file.txt' mode='w' encoding='cp1252'>
file opened successfully
Example : program to open file in append mode
- if file not present, it will create new file and open the file.
- if file exist, it will open file and open the file without deleting the previous data and the new dat will be added to end of existing data.
#program
file = open("my_file.txt", "a")
print(file)
print("file opened successfully")
file.close()
#Output
<_io.TextIOWrapper name='my_file.txt' mode='a' encoding='cp1252'>
file opened successfully
- How to Read data in File read()
data_in_file = file_name.read()
#program
file = open("my_file.txt", "r")
print("file opened successfully")
data_in_File = file.read()
print("below is the data inside the file\n\n",data_in_File)
file.close()
#Output
file opened successfully
below is the data inside the file
Hi, this is Techno Xpresss.
This is a sample file to read.
- How to Write data in File write()
file_name.write()
#program
file = open("my_file.txt", "r")
file_1 = open("my_file_1.txt", "w")
print("file opened successfully")
data_in_File = file.read()
file_1.write(data_in_File)
print("data written successfully")
file.close()
file_1.close()
file_1 = open("my_file_1.txt", "r")
data_in_File = file_1.read()
print("\n\nbelow is the data inside the file_1\n\n",data_in_File)
file_1.close()
#Output
file opened successfully
data written successfully
below is the data inside the file_1
Hi, this is Techno Xpresss.
This is a sample file to read.
Comments
Post a Comment