Python Files
File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk).
You can open and use files for reading or writing by creating an object of the file class and using its read, readline or write methods appropriately to read from or write to the file. The ability to read or write to the file depends on the mode you have specified for the file opening. Then finally, when you are finished with the file, you call the close method to tell Python that we are done using the file
File Object Methods
Open: it is used to open a file.
Syntax: open(filename,mode)
Modes:
W writing
R reading
Wb write binary
Rb read binary
A appending
B binary
Python File Methods
Method |
Description |
close() |
Close an open file. It has no effect if the file is already closed. |
detach() |
Separate the underlying binary buffer from the TextIOBase and return it. |
fileno() |
Return an integer number (file descriptor) of the file. |
flush() |
Flush the write buffer of the file stream. |
read(n) |
Read atmost n characters form the file. Reads till end of file if it is negative or None. |
readable() |
Returns True if the file stream can be read from. |
readline(n=-1) |
Read and return one line from the file. Reads in at most n bytes if specified. |
readlines(n=-1) |
Read and return a list of lines from the file. Reads in at most n bytes/characters if specified. |
seek(offset,from=SEEK_SET) |
Change the file position to offset bytes, in reference to from (start, current, end). |
seekable() |
Returns True if the file stream supports random access. |
tell() |
Returns the current file location. |
truncate(size=None) |
Resize the file stream to size bytes. If size is not specified, resize to current location. |
writable() |
Returns True if the file stream can be written to. |
write(s) |
Write string s to the file and return the number of characters written. |
writelines(lines) |
Write a list of lines to the file. |
Program to read the data from the text file
f=open("test.py","r")
k=1
print(f.read())
program to read the data from text file only given lenghth
f=open("test.py","r")
k=1
print(f.read(10))
program on readline
f=open("test.py","r")
k=len(f.readlines()) # returns number of lines
i=0
f.close()
f=open("test.py","r")
while(i<k):
s=f.readline()
print(s)
i=i+1
f.close()
program to write the data from one file to another file
f=open("test.py","r")
f1=open("test1.py","w")
while(True):
s=f.readline()
f1.write(s)
if s=="":
break;
f.close()
f1.close()
create the student data file
f=open("student.txt","w")
ch="y"
while(ch!="n"):
na=input("enter name")
mat=int(input("enter maths"))
phy=int(input("enter phy"))
che=int(input("enter chemi"))
tot=mat+phy+che
ave=tot/3
s=na+str(mat)+str(phy)+str(che)+str(tot)+str(ave)+"\n"
f.write(s)
ch=input("Continue y/n")
f.close()
program to read the student data file
f=open("student.txt","r")
while(True):
s=f.readline()
print(s)
if s=="":
break
f.close()
program to read the student data file using for loop
f=open("student.txt","r")
for i in f:
print(i.rstrip()) # rstrip removes unwanted line breaks
with statement
Stream objects have an explicit close() method, but what happens if our code has a bug and crashes before we call close()? That file could theoretically stay open for longer than necessary.
Probably, we could use the try..finally block. But we have a cleaner solution, which is now the preferred solution in Python 3: the with statement:
Programe on with
with open("student.txt","r") as f:
f.seek(5) # move to 5 the character
c=f.read(3) # reads 3 characters
print (c)
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743