File : .txt,.c,.java etc
Database : oracle,mysql,mariadb
Big data : hadoop

Two type of file in python

  • Binary files: mp3 , image , pdf , video , audio
  • Text files: .txt , .c , .java

3 Mode in python File Handing

  • r : read mode
  • w : write mode
  • a : append mode
Character Meaning
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
1.open the file
		open() function
2.read or write or append operations
		read() readLine() readLines()
		write()
3.close the file
		close() function

NOTE 1 :In Python file handing default operations mode is read

NOTE 2 :In Python file handing if you read the data on file.file is mandatory

if you preform write operations in file.file exits ya not exits is not mandatory in python

file=open("index.html","w")
file.write("Hello Sir \n this is javablackbook website")
file.close()
print("operations are completed")

if you preform read operations in python.file exits is mandatory in python

try:
	file=open("index.html")
	data=file.read()
	print(data)
	file.close()
except IOError as a:
	print("Operations fail : ",a)
print("Operations are completed")

Here remove r args in open("html") method because in python default opreation is read and index.html file exits

try:
	file=open("index.html")
	data=file.read()
	print(data)
	file.close()
except IOError as a:
	print("Operations fail : ",a)
print("Operations are completed")
this is javablackbook website
Operations are completed

Here remove r args in open("html") method and html file not exits

try:
	file=open("html")
	data=file.read()
	print(data)
	file.close()
except IOError as a:
	print("Operations fail : ",a)
print("Operations are completed")
Operations fail :  [Errno 2] No such file or directory: 'html'
Operations are completed

append opreation in python.""index.html"" file is already available then data is appended

try:
	file=open("index.html","a")
	data=file.write("\n keep doing and keep movie")
	file.close()
except IOError as e:
	print("Operations fail : ",e)
print("Operations are completed")
Operations are completed 

append opreation in python.view.html file is not available then create the file write the data

try:
	file=open("view.html","a")
	data=file.write("\n keep doing and keep movie")
	file.close()
except IOError as e:
	print("Operations fail : ",e)
print("Operations are completed")

How to get file mode,name and close or not in pyhton

try:
	file=open("index.html","w")
	print(file)
	print(file.name)
	print(file.mode)
	print(file.closed)
	file.close()
	print(file.closed)
except IOError as e:
	print("Operations fail : ",e)
print("Operations are completed")
<_io.TextIOWrapper name='index.html' mode='w' encoding='cp1252'>
index.html
w
False
True
Operations are completed