Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Read the whole content of a text file

$ cat input.txt
First line.
Second line.
Last line.
(1) read the whole content to a string
f = open("input.txt", "r")

content = f.read()

f.close()

print("'" + content + "'")
let content = readFile("input.txt")

echo "'" & content & "'"

Output:

'First line.
Second line.
Last line.
'
(2) read the whole content to a list/seq
f = open("input.txt", "r")

lines = f.read().splitlines()

f.close()

print(lines)
# ['First line.', 'Second line.', 'Last line.']
import std/strutils


let v1 = readFile("input.txt").splitlines()
echo v1
# @["First line.", "Second line.", "Last line.", ""]

let v2 = readFile("input.txt").strip().splitlines()
echo v2
# @["First line.", "Second line.", "Last line."]

Notice the subtle difference:

  • Python: when you use .splitlines(), at the end of the resulting list there is no empty string.
  • Nim: at the end of the seq (v1) there is an empty string. So .splitlines() must just call .split('\n'). If you don't need that empty string, then combine splitlines() with strip() .
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 06, 22:29