Python File IO Basics
Python File IO Basics
#25
Non volitile: which does not gets vanish when laptop is turned off .
volitile: which does gets vanish when laptop is turned off .
- r : r mode opens a file for read.-default
- w : w mode opens a file for read.
- x : x creates a new file. It does not work for an already existing file, as in such cases the operation fails.
- a : a for append, which means to add something to the end of the file. It just adds the data we like in write(w) mode but instead of overwriting it just adds it to the end of the file
- t : used to open our file in text mode. It deals with the file data as a string.-default
- b : b for binary and this mode can only open the binary files, that are read in bytes. The binary files include images, documents, or all other files that require specific software to be read.
- + :we can read and write a file simultaneously. The mode is mostly used in cases where we want to update our file.
#26
Open(), Read(), Readline()
#To read:
# f = open("aryan.txt")
# content = f.read()
# print(content)
#
# f.close()
# To read in Binary
# f = open("aryan.txt", "rb")
# content = f.read()
# print(content)
#
# f.close()
# To read in Text
# f = open("aryan.txt", "rt")
# content = f.read()
# print(content)
#
# f.close()
# To read character by character
# f = open("aryan.txt", "rt")
# content = f.read()
# for line in content:
# print(line)
#
# f.close()
# To read every line
# f = open("aryan.txt", "rt")
# for line in f:
# print(line, end="")
#
# f.close()
# To readline
# f = open("aryan.txt", "rt")
# print(f.readline())
# print(f.readline())
# print(f.readline())
# f.close()
# To read in list form
# f = open("aryan.txt", "rt")
# print(f.readlines())
# f.close()
# to read and write
#f= open("aryan.txt", "r+" )
# print(f.readline())
# print(f.readline())
# print(f.readline())
# print(f.readline())
# f.close()
# for writing in a file
# print(f.read())
# f.write("Hello0000000000000000000000\n")
# f.close()
Comments
Post a Comment