File : .txt,.c,.java etc Database : oracle,mysql,mariadb Big data : hadoop
| '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
file=open("index.html","w")
file.write("Hello Sir \n this is javablackbook website")
file.close()
print("operations are completed")
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
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
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")
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