Unit 8 : File Processing Flashcards
What does open() returns?
- “file handle” - a variable used to perform operations on the file
- LIke “File -> Open” in a aword processor
What does open() accepts? ( 2 )
handle = open( filename, mode )
What are the mode for file handle?
- r - to open a file for reading
- w - to open a file for writing if file exist, otherwise create a file
- a - to open a file for append if file exist, otherwise create a file
What can we seperate the data?
- Using seperator ( , )
How can we write into file?
name = input(“Name : “)
f = open(“user.txt”, “a”)
f.write(name)
f.close
print(“Record Added”)
How can we read from file?
f = open(“user.txt” , “r” )
for line in f:
print(line)
How can we count line in a file?
f = open(“user.txt”)
count = 0
for line in f:
count = count + 1
print(“Line Count : “ , count )
How can we search through a file? ( .startswith )
f = open(“user.txt”, “r”)
for line in f:
if line.startswith(“Jill”):
print(line)
How can we search through a file? ( rstrip() )
f = open(“user.txt” , “r” )
for line in line:
line = line.rstrip()
print(line)
How can we skip a lline?
f = open(“user.txt”,”r”)
for line in f:
line = line.rstrip()
if line.startswith(“Jack”):
continue
print(line)
How can we select line ( in ) ?
f = open (“user.txt”, “r”)
for line in f:
line = line.rstrip()
if “jim@mail.com” in line:
print(line)