ktolkun
9/11/2018 - 8:42 PM

Working with files

Files (open, read, write), path of file

import os
# reading the file
inf = open("input.txt", "r", encoding="utf-8")
s1 = inf.readline()
s2 = inf.readline()
inf.close()

filePath = os.path.join(".", "input.txt")  # point general (for all operation systems) path to your file
with open(filePath, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        print(line)
    # file will be closed automatically


# writing the file
outf = open("output.txt", "w", encoding="utf-8")
outf.write("Hello, World!\n")
outf.write(str(25) + "\n")
outf.close()

with open("output.txt", "w", encoding="utf-8") as outf:
    outf.write("Goodbye, World!")