0

I need a program in Python, which would read from a file and print out title words on the screen. I started doing so and this is what I have so far:

info = open("info.txt", "r")
lines = info.readlines()

def function():
    while True:
        if lines.istitle():
            print (lines)
        if not lines.istitle():
            break

But it does not seem to work. Any suggestions?

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Johyung
  • 11
  • 1
    I suggest you take a look at the [official tutorial's section on file I/O](https://docs.python.org/3.4/tutorial/inputoutput.html#reading-and-writing-files). – TigerhawkT3 May 22 '15 at 23:32
  • 1
    The `break` statement should probably be `pass`. `break` stops the loop, and you will only print the first title you find. – Peter Wood May 22 '15 at 23:50
  • You really need to show us a few lines of your file (and what exactly the output should be for it). – Stefan Pochmann May 22 '15 at 23:54
  • Actually @Peter, not only will it not get to the first title, it'll crash, because it tries to call `istitle()` on a `list`. – TigerhawkT3 May 23 '15 at 00:06
  • 1
    @TigerhawkT3 Not true! The function is never called, so it doesn't crash! – Stefan Pochmann May 23 '15 at 00:07
  • 2
    Dang eh - this shows why the poster _really_ needs to describe "what is wrong", as recommended by the guidelines at [help]. In this case, a traceback should have been posted, or the symptom of "what doesn't work" :) – GreenAsJade May 23 '15 at 00:08
  • 3
    Won't crash, and doesn't print anything it isn't supposed to - looks good to me! – TigerhawkT3 May 23 '15 at 00:08
  • 1
    @TigerhawkT3: That's why I use `#!/usr/bin/true` on all of my scripts. :) – abarnert May 23 '15 at 00:31

4 Answers4

2
  info = open("info.txt", "r")
  res = [line for line in info if line.istitle()]
  print res
  info.close() 

or more simple

  info = open("info.txt", "r")
  for line in info:
      if line.istitle():
         print line
  info.close()
2

You can define a function which yields title lines from a file:

def titles(filename):
    with open(filename) as info:
        for line in info:
            if line.istitle():
                yield line

And then print them all:

for title in titles('info.txt'):
    print(title)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

This is one way to do it:

info = [ln.strip() for ln in open("info.txt") if ln.istitle()]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • @StefanPochmann I maybe totally wrong but my understanding is that in cPython the file will be closed as soon as the list comprehension finishes and Python standard does not require that it be closed. – Joe T. Boka May 23 '15 at 00:24
  • Hmm. I must admit I'm not sure about that and don't see an easy way to tell. Will investigate a little. That -1 is not mine, btw. – Stefan Pochmann May 23 '15 at 00:28
  • @StefanPochmann ok thanks, I will look into it a little bit more as well but I have seen this type of list comprehension before and it supposed to be ok like this – Joe T. Boka May 23 '15 at 00:32
  • You appear to be correct about cPython. I went through `gc.get_objects()` right after the comprehension and there was no file. And I did find one when using `f=open(...)`. I also found [this question](http://stackoverflow.com/questions/18840880/will-using-list-comprehension-to-read-a-file-automagically-call-close) and others, where the answers also say in cPython it closes immediately, but in other implementations it might not. I'm undecided but will probably stick with the context manager. I apologize for my not fully educated initial remark, though. Learned something again, thanks. – Stefan Pochmann May 23 '15 at 00:42
0

This is similar to @user3238855's answer, but I'm using Python 3's print() function, with a filter() instead of a comprehension, and not saving a reference to a list of lines.

with open('info.txt', 'r') as f:
    print(*filter(str.istitle, f), sep='\n')
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97