Uhm.
I think you desperately need to realize how Python works.
file.readline() - reads ONE line.
file.readlines() - reads ALL lines, and splits them by line delimiter.
Now, imagine:
f = file("/etc/motd", "r")
print f.readline()
Will that print the first line or first character of that file? The first line.
Consider this:
for line in f.readline():
print line
This will _of course_ print each charater, since **iterating str objects returns each character of them in a sequence!**
Comment
i seem to have a problem with readlines()as below, am i doing anything wrong?
i have
global f
f=open("hello.txt", "r")
f1 = open("C:\\log1.txt",'w')
f2 = open("C:\\log2.txt",'w')
def new1():
for line in f.readlines():
if line.find('raj')>= 0:
f1.write("%s" %line)
def new2():
for line in f.readlines():
if line.find('abc')>= 0:
f2.write("%s" %line)
new1()
new2()
i don't seem to get an output for new2 ,i mean it doesn't go through the for loop
Am i doing anything wrong ??
Parent comment
Uhm. I think you desperately need to realize how Python works. file.readline() - reads ONE line. file.readlines() - reads ALL lines, and splits them by line delimiter. Now, imagine: f = file("/etc/motd", "r") print f.readline() Will that print the first line or first character of that file? The first line. Consider this: for line in f.readline(): print line This will _of course_ print each charater, since **iterating str objects returns each character of them in a sequence!**
Replies
In new1 you read the file f to the end.
and when you come to new2
f is at end of file.
Thats why
Before new2()
f.close()
f=open("hello.txt", "r")