with statement is a context management method for opening and closing files. For example, the general opening posture is
file = open("filename", 'wb')
# do something
file.close()
With context management, withAfter the code block is executed, the internal method will be called to directly close the file. There is no need to manually call the close() method, which is the way given in the question. Of course format is a method of formatting strings. The position of {n} is reserved in the string. n will be used as the key of a keyword parameter. After the value is passed in, the passed in will be used. valuereplacement, so the position of {n:02d} in the question will be replaced by the integer string received by n. If only {} is left in the string, and no key value is specified, then the parameter passed in format will be used as a positional parameter, and will be filled in order one by one corresponding to the {} in the string.
The
with
statement is a context management method for opening and closing files. For example, the general opening posture isWith context management,
with
After the code block is executed, the internal method will be called to directly close the file. There is no need to manually call theclose()
method, which is the way given in the question.Of course
format
is a method of formatting strings. The position of{n}
is reserved in the string.n
will be used as the key of a keyword parameter. After the value is passed in, the passed inwill be used. value
replacement, so the position of{n:02d}
in the question will be replaced by the integer string received byn
. If only{}
is left in the string, and no key value is specified, then the parameter passed informat
will be used as a positional parameter, and will be filled in order one by one corresponding to the{}
in the string.Format string
Equivalent to:
"map" + "00" + ".png"