diff --git a/doc/tutorial/machine_learning_map/svg2imagemap.py b/doc/tutorial/machine_learning_map/svg2imagemap.py index 0e6395cfb54..80f06c8fb97 100644 --- a/doc/tutorial/machine_learning_map/svg2imagemap.py +++ b/doc/tutorial/machine_learning_map/svg2imagemap.py @@ -107,5 +107,5 @@ for g in parsed_groups: ) ) -outfile = open(sys.argv[1].replace(".svg", ".html"), "w") -outfile.write("\n".join(out)) +with open(sys.argv[1].replace(".svg", ".html"), "w") as f: + f.write("\n".join(out)) diff --git a/doc/tutorial/text_analytics/data/languages/fetch_data.py b/doc/tutorial/text_analytics/data/languages/fetch_data.py index a4f577bd7a7..2dd0f208ade 100644 --- a/doc/tutorial/text_analytics/data/languages/fetch_data.py +++ b/doc/tutorial/text_analytics/data/languages/fetch_data.py @@ -54,7 +54,8 @@ for lang, page in pages.items(): # downloading a couple of articles should not be considered abusive request.add_header('User-Agent', 'OpenAnything/1.0') html_content = opener.open(request).read() - open(html_filename, 'wb').write(html_content) + with open(html_filename, 'wb') as f: + f.write(html_content) # decode the payload explicitly as UTF-8 since lxml is confused for some # reason @@ -73,7 +74,8 @@ for lang, page in pages.items(): text_filename = os.path.join(text_lang_folder, '%s_%04d.txt' % (lang, i)) print("Writing %s" % text_filename) - open(text_filename, 'wb').write(content.encode('utf-8', 'ignore')) + with open(text_filename, 'wb') as f: + f.write(content.encode('utf-8', 'ignore')) i += 1 # split the paragraph into fake smaller paragraphs to make the @@ -93,8 +95,8 @@ for lang, page in pages.items(): short_text_filename = os.path.join(short_text_lang_folder, '%s_%04d.txt' % (lang, j)) print("Writing %s" % short_text_filename) - open(short_text_filename, 'wb').write( - small_content.encode('utf-8', 'ignore')) + with open(short_text_filename, 'wb') as f: + f.write(small_content.encode('utf-8', 'ignore')) j += 1 if j >= 1000: break diff --git a/examples/applications/wikipedia_principal_eigenvector.py b/examples/applications/wikipedia_principal_eigenvector.py index e0949cb592d..fcc337b0a4e 100644 --- a/examples/applications/wikipedia_principal_eigenvector.py +++ b/examples/applications/wikipedia_principal_eigenvector.py @@ -65,7 +65,8 @@ for url, filename in resources: if not os.path.exists(filename): print("Downloading data from '%s', please wait..." % url) opener = urlopen(url) - open(filename, "wb").write(opener.read()) + with open(filename, "wb") as f: + f.write(opener.read()) print()