0

I have two files in a directory. I'd like to read the lines from each of the files. Unfortunately when I try to do so with the following code, there is no output.

from pathlib import Path 

p = Path('tmp')
for file in p.iterdir():
   print(file.name)

functions.py
test.txt

for file in p.iterdir():
    f = open(file, 'r')
    f.readlines()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
jgg
  • 791
  • 4
  • 17
  • 1
    lines = f.readlines() – Tarik Oct 13 '21 at 19:31
  • 2
    There's no output because you're not outputting anything. Did you mean `print(f.readlines())`? – wjandrea Oct 13 '21 at 19:32
  • 2
    BTW, best practice for opening files is a with-statement: `with open(file) as f: ...` – wjandrea Oct 13 '21 at 19:34
  • 2
    Side-note: You rarely want to actually call `.readlines()`; usually, you just iterate the lines and process them as you go (`for line in f: print(line)`), and avoid slurping the whole file into memory. When you do need the whole file in memory, you frequently need it in some form other than a raw `list`, and avoiding `.readlines()` in favor of `sorted(f)`, `set(f)`, etc., or a loop that builds the data structure line by line (avoiding duplication or coalescing values in some other manner) rather than making a temporary `list` first, then building a new structure from it. – ShadowRanger Oct 13 '21 at 19:44

3 Answers3

1

You should print data like this from text.py

count = 1
f = open(file, 'r')
Lines = f.readlines()
for line in Lines:
    count += 1
    print("Line {}: {}".format(count, line.strip()))

Output will look like:

Line 1: ...
Line 2: ...
Line 3: ...

you can see reading line example here - Line reading

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • There are a lot of improvements you could make here: 1) Best practice for opening files is a `with` statement. 2) *UpperCamelCase* variable names (`Lines`) should be reserved for classes. 3) Don't bother with `f.readlines()` when you could iterate over `f` directly. 4) `enumerate(start=1)` instead of counting manually. 5) f-strings are simpler, but that's nbd. `with open(file) as f: for count, line in enumerate(lines, 1): print(f"Line {count}: {line.strip()}")` – wjandrea Oct 13 '21 at 20:00
1

You could use fileinput:

import os
import fileinput

for line in fileinput.input(os.listdir('.')):
    print(line)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
1

You're reading all the lines from the file, but you're not outputting them. If you want to print the lines to standard output, you need to use print() as you did in your first example.

You can also write this somewhat more elegantly using contexts and more iterators:

from pathlib import Path 

file = Path('test.txt')
with file.open() as open_file:
    for line in open_file:
        print(line, end="")

test.txt:

Spam
Spam
Spam
Wonderful
Spam!
Spamity
Spam

Result:

Spam
Spam
Spam
Wonderful
Spam!
Spamity
Spam

Using a context for opening the file (with file.open()) means you inherently set up closing the file, and the iterator for the lines (for line in open_file) means you're not loading the whole file at once (an important consideration with larger files).

Setting end="" in print() is optional depending on how your source files are structured, as you might otherwise end up printing extra blank lines in your output.

Da Chucky
  • 781
  • 3
  • 13
  • Note that using `print(line, end="")` is equivalent to just doing `sys.stdout.write(line)`; given that, if you just want to copy all the lines from input to `sys.stdout`, you could replace `for line in open_file: print(line, end="")` with just `sys.stdout.writelines(open_file)`; `open_file` is an iterable of strings, and `writelines` writes from an iterable of strings (lazily; this won't slurp the whole input into memory, it'll just read and write each line as it goes). – ShadowRanger Oct 14 '21 at 16:04