A
A
Alexey Denisov2020-08-04 19:27:36
Python
Alexey Denisov, 2020-08-04 19:27:36

Open and Write to file?

Tell me what is the difference in the above code,
and is it required to close the file in both cases?

f = open(path, 'w')
   f.write(req.text)


with open(path, 'w') as f:
    f.write(req.text)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
soremix, 2020-08-04
@SoreMix

In the first version, the indents are superfluous. Will have to close the file after work.
In the second case, the context manager is used, the file will close itself after the end of the with ... as
block The second option is considered more correct

A
Alexander Pikeev, 2020-08-04
@Baryon

In the first case, you will need to manually close the file f.close(), and in the second, it closes after it is no longer used. More precisely, it will close after the execution of the last line of code in the block. Indents in the first option are not needed.

I
IceJOKER, 2015-02-26
@IceJOKER

ReferenceError: submit is not defined
Look in the console and click to find out what error it gives.
no declaration function submit()

C
Centrino, 2015-02-26
@Centrino

What are you trying to submit()? It is not announced what needs to be submitted.
If you want to do it with regular javascript, then give the form a name, for example name = "registrationForm" and write in the onClick event = "registrationForm.submit()"
You can also use $('#form').submit().
Better yet, assign IDs to your form and "link-button" and fire a jquery event when a specific button is clicked.
If you are already using jquery then why are you trying to use the good old dom.

G
Gosha Mann, 2015-02-26
@IgorMann

Instead of
Write
<input type="submit" value="Регистрация">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question