IKH

In Built Function

The seek() and tell() methods

tell()

  • ==>We can use tell() method to return current position of the cursor(file pointer) from
  • beginning of the file. [ can you plese telll current cursor position]
  • The position(index) of first character in files is zero just like string index.

Example

Python
f=open("abc.txt","r") 
print(f.tell()) 
print(f.read(2)) 
print(f.tell()) 
print(f.read(3)) 
print(f.tell()) 

Output

PowerShell
6

abc.txt

sunny
bunny
chinny
vinny

Output

0
su
2
nny
5

seek()

  • We can use seek() method to move cursor(file pointer) to specified location.
  • [Can you please seek the cursor to a particular location]
  • f.seek(offset, fromwhere)
  • offset represents the number of positions
  • The allowed values for second attribute(from where) are
  • 0—->From beginning of file(default value)
  • 1—->From current position
  • 2—>From end of the file

Note

  • Python 2 supports all 3 values but Python 3 supports only zero.

Example

Python
data="All Students are STUPIDS" 
f=open("abc.txt","w") 
f.write(data) 
with open("abc.txt","r+") as f: 
text=f.read() 
print(text) 
print("The Current Cursor Position: ",f.tell()) 
f.seek(17) 
print("The Current Cursor Position: ",f.tell()) 
f.write("GEMS!!!") 
f.seek(0) 
text=f.read() 
print("Data After Modification:") 
print(text) 
 
Output
 
All Students are STUPIDS 
The Current Cursor Position: 24 
The Current Cursor Position: 17 
Data After Modification:
All Students are GEMS!!!

Output

PowerShell
5

How to check a particular file exists or not?

  • We can use os library to get information about files in our computer.
  • os module has path sub module,which contains isFile() function to check whether a
  • particular file exists or not?
  • os.path.isfile(fname)

Q. Write a program to check whether the given file exists or not. If it is
available then print its content?

Python
import os,sys 
fname=input("Enter File Name: ") 
if os.path.isfile(fname): 
print("File exists:",fname) 
f=open(fname,"r") 
else: 
print("File does not exist:",fname) 
sys.exit(0) 
print("The content of file is:") 
data=f.read() 
print(data) 
 
Output
D:\Python_classes>py test.py 
Enter File Name: durga.txt 
File does not exist: durga.txt 
 
D:\Python_classes>py test.py 
Enter File Name: abc.txt 
File exists: abc.txt 
The content of file is: 
All Students are GEMS!!! 
All Students are GEMS!!! 
All Students are GEMS!!! 
All Students are GEMS!!! 
All Students are GEMS!!! 
All Students are GEMS!!!

Note

  • sys.exit(0) ===>To exit system without executing rest of the program.
  • argument represents status code . 0 means normal termination and it is the default value.

Q. Program to print the number of lines,words and characters present in the
given file?

Python
import os,sys 
fname=input("Enter File Name: ") 
if os.path.isfile(fname): 
print("File exists:",fname) 
f=open(fname,"r") 
else: 
print("File does not exist:",fname) 
sys.exit(0) 
lcount=wcount=ccount=0 
for line in f: 
lcount=lcount+1 
ccount=ccount+len(line) 
words=line.split() 
wcount=wcount+len(words) 
print("The number of Lines:",lcount) 
print("The number of Words:",wcount) 
print("The number of Characters:",ccount) 
 
Output
D:\Python_classes>py test.py 
Enter File Name: durga.txt 
File does not exist: durga.txt 
 
D:\Python_classes>py test.py 
Enter File Name: abc.txt 
File exists: abc.txt 
The number of Lines: 6 
The number of Words: 24 
The number of Characters: 149 

abc.txt

  • All Students are GEMS!!!
  • All Students are GEMS!!!
  • All Students are GEMS!!!
  • All Students are GEMS!!!
  • All Students are GEMS!!!
  • All Students are GEMS!!!

Report an error