erictleung,
@erictleung@mastodon.social avatar

Really cool to see this concise way to read text from a file in Python using the built-in pathlib module.

from pathlib import Path
line_lst = Path("to/the/file.txt").read_text().splitlines()

https://stackoverflow.com/a/71631196/6873133

cazabon,

@erictleung

Looks like you want it as lines. You can do it simpler:

for line in Path("/path/to/file").open("r"):
do_something_with_line(line)

The lines will still have their EOLs, but you can rstrip() them to get rid of it.

This also has the advantage of processing the input file in streaming fashion; it doesn't have to read the whole file into memory at once.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming
  • cubers
  • magazineikmin
  • Durango
  • cisconetworking
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • rosin
  • kavyap
  • ethstaker
  • thenastyranch
  • everett
  • DreamBathrooms
  • JUstTest
  • ngwrru68w68
  • anitta
  • GTA5RPClips
  • normalnudes
  • InstantRegret
  • tacticalgear
  • modclub
  • mdbf
  • provamag3
  • Leos
  • tester
  • megavids
  • lostlight
  • All magazines