Write to a file (Python)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
#! /usr/bin/python3
#
# Download Channable-XML-file - Strompf, Feb. 2019
#############################################################################
#
import urllib.request


#############################################################################
# Read
#############################################################################
#
url = 'https://www.example.com/fr-fr_channable.xml'
response = urllib.request.urlopen(url)
file_content_raw = response.read()      # a `bytes` object
file_content_decodes = file_content_raw.decode('utf-8') # a `str`; this step can't be used if data is binary


#############################################################################
# Write
#############################################################################
#
# testvar="Hello, world!"

f = open('testfile-02','w')
f.write(file_content_decodes)
f.close()

Bronnen