-1

I'm new to python. I don't know why when I use str.read(), all character store in each line like this here

while True:
    fhand = input('Enter a file name:\n')
    try:
        fname = open(fhand)
        break
    except:
        print('Please enter file name again in doc C')
    continue
fr = fname.read()
for line in fr:
    line = line.rstrip()
    if not line.startwith('From')
        print(line)
nulladdr
  • 741
  • 5
  • 15
Ace
  • 1
  • 1
  • change `fr = fname.read()` to `fr = fname.read().splitlines()` – Paul M. Mar 21 '20 at 01:13
  • 1
    Put your code as text in your answer instead of an image – jamylak Mar 21 '20 at 01:14
  • 1
    You should just iterate over the file object directly to iterate over the lines. In any case, please read [ask] and the [help]. Questions seeking debugging help must provide a [mcve], and that must be self-contained in the question. Post all code as [*formatted text*.](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) – juanpa.arrivillaga Mar 21 '20 at 01:25
  • 1
    @user10987432 that is an anti-pattern. If you really wanted that, you should just sue `fr = list(fname)`, but almost certainly, you can just use the file-object directly to not read the whole thing in to memory. – juanpa.arrivillaga Mar 21 '20 at 01:25
  • Be careful about using a bare `except` like this, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. Also, I would recommend using a context manager to handle file objects. – AMC Mar 21 '20 at 02:17

1 Answers1

0

instead of fname.read() use fname.readlines()

https://www.tutorialspoint.com/python/file_readlines.htm

this
  • 191
  • 13