Read and write files with ‘with’ statement in Python

I always keep forgetting how exactly with statement works with opening and reading the files in Python. path = ‘./path/filename.txt’ with open(path,’r’) as file: data = file.readlines() print(data) Or if you would like to avoid getting ‘\n’ after each line when using .readlines() you can use this instead: data = file.read().splitlines() Opening files with ‘with’ … Read more