Can anyone tell me is it right syntax? For Open: f = open("file name", file_mode) f is file object f = open("file.txt", w)
For Close: f = open("file.txt",'w') f.close() # close a file
If it does not then tell me the exact syntax of how to create it. I just see this same syntax to the other side here but when I coded it's not working.
Mark
The better way is
with open("file name", "r") as fh: content = fh.read()Then it's automatically closed after the scope ends.
As a bonus tip,
.read().splitlines()is better than.readlines()because it also stripts the newline characters.