I am having problems writing to a file where I am using pylast. Following a template given in pylast, I added a regular expression to extract what I need (which is doing ok), but when I tried to print to a file, I get an error, and don’t know how to fix it (I am teaching myself python and some of its libraries).
I suspect there is an encoding specification I need to make somewhere (some of the output to screen also shows non-standard characters). I don’t know how to solve my problem.
Can anybody help?
Thanks
import re
import pylast
RawArtistList = []
ArtistList = []
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account for Last.fm
API_KEY = "XXX"
API_SECRET = "YYY"
###### In order to perform a write operation you need to authenticate yourself
username = "username"
password_hash = pylast.md5("password")
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)
## _________INIT__________
COUNTRY = "Germany"
#---------------------- Get Geo Country --------------------
geo_country = network.get_country(COUNTRY)
#---------------------- Get artist --------------------
top_artists_of_country = str(geo_country.get_top_artists())
RawArtistList = re.findall(r"u'(.*?)'", top_artists_of_country)
top_artists_file = open("C:\artist.txt", "w")
for artist in RawArtistList:
print artist
top_artists_file.write(artist + "\n")
top_artists_file.close()
The name of the file I am trying to create “artist.txt” changes to “x07rtist.txt” and the error kicks in. I get this:
Traceback (most recent call last):
File "C:\music4A.py", line 32, in <module>
top_artists_file = open("C:\artist.txt", "w")
IOError: [Errno 22] invalid mode ('w') or filename:'C:\x07rtist.txt'
Thank you very much for any help! Cheers.
The Python docs say:
…so when you say
that string literal is being interpreted as
…where
\ais a single character that has a value of 0x07.…that line should instead be:
or
or