问题描述:

I want to add list of mp3 files to iTunes via a applescript. The list of files is generated in python, where I also construct the applescripts. Here one example:

osascript -e tell application "iTunes" ...

to add POSIX file "SomeMP3FileWithSpecialCharacters"!$%&$.mp3"

But if the file name contains special characters, it does not work. I think I have to escape the filenames in Python for applescript. So how would I do that the most pythonic way?

网友答案:

One thing you might want to try would be to cast your entire path to a string:

str('filepath_here')

If that doesn't work, probably the next easiest way would be to use python's regular expressions module like so:

>>> import re
>>> re.escape('filepath_with_bad_characters!//%')
'filepath\\_with\\_bad\\_characters\\!\\/\\/\\%'

Alternately, you could use re.sub to only escape certain characters. The re module documentation has more information.

相关阅读:
Top