Answer the question
In order to leave comments, you need to log in
What is the best way to create an empty file in Python 3?
Method 1
with open('textfile_1.txt', 'tw', encoding='utf-8') as f:
pass
open('textfile_2.txt', 'tw', encoding='utf-8')
f = open('textfile_3.txt', 'tw', encoding='utf-8')
f.close()
Answer the question
In order to leave comments, you need to log in
I would suggest such an option
open (..). close ()
and the file is closed, and in one line and without clever constructions.
As for errors, all options in this regard are the same.
Contexts are more in line with zen and also ensure that the file is closed when an error occurs. (this is the first way)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question