Escape whitespace in paths using nautilus script

Posted by Tommy Brunn on Stack Overflow See other posts from Stack Overflow or by Tommy Brunn
Published on 2010-06-05T12:06:29Z Indexed on 2010/06/05 12:12 UTC
Read the original article Hit count: 228

Filed under:
|

I didn't think this would be as tricky as it turned out to be, but here I am. I'm trying to write a Nautilus script in Python to upload one or more images to Imgur just by selecting and right clicking them. It works well enough with both single images and multiple images - as long as they don't contain any whitespace. In fact, you can upload a single image containing whitespace, just not multiple ones.

The problem is that NAUTILUS_SCRIPT_SELECTED_FILE_PATHS returns all the selected files and directories as a space separated string. So for example, it could look like this:

print os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']
/home/nevon/Desktop/test image.png /home/nevon/Desktop/test.jpg

What I need is a way to -either in bash or Python- escape the spaces in the path - but not the spaces that delimit different items. Either that, or a way to put quotation marks around each item.

The ultimate solution would be if I could do that in bash and then send the items as separate arguments to my python script. Something like:

python uploader.py /home/nevon/Desktop/test\ image.png /home/nevon/Desktop/test.jpg

I've tried RTFM'ing, but there doesn't seem to be a lot of good solutions for this. At least not that I've found. Any ideas?

© Stack Overflow or respective owner

Related posts about python

Related posts about nautilus