Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape whitespaces in linux path and file names

Tags:

python

regex

file

i actually cleaning up my system. And as usual i am trying to do it the python way, so i am cleaning up duplicates in my Music library. And now i am trying to find a pattern for re module to escape all whitespace and special characters. But unfortunately i was unable to find something useable. This is some snipped of the duplicates list i generated with linux cli tool called "fdupes" :

./D/dominik eulberg/01 bionik/01 bionik (guy gerber remi.m4p
./D/dominik eulberg/05 dominik eulberg/01 bionik/01 bionik (guy gerber remi.m4p

./N/Nightmares On Wax/Smokers Delight/05 - Stars.mp3
./N/Nightmares On Wax/Smokers Delight/NOW - smokers delight_04.MP3

./N/Nightmares On Wax/Smokers Delight/13 - What I'm Feelin (Good).mp3
./N/Nightmares On Wax/Smokers Delight/NOW - smokers delight_13.MP3

./N/Nightmares On Wax/Smokers Delight/16 - Gambia Via Vagatorbeach.mp3
./N/Nightmares On Wax/Smokers Delight/NOW - smokers delight_16.MP3
./S/Stupidozid/Morast_Beatz/12. Morast Beatz - RAF - von TeeK.mp3
./S/stupidozid_12/Morast_Beatz-Startproblem_und_Endloesung_2012/12. Morast Beatz - RAF - von TeeK.mp3

./S/Slackers/Folder.jpg
./S/Slackers/AlbumArt_{4CC84D50-8EED-415A-9ED3-7144D9AE90F2}_Large.jpg

./S/Slackers/AlbumArtSmall.jpg
./S/Slackers/AlbumArt_{4CC84D50-8EED-415A-9ED3-7144D9AE90F2}_Small.jpg

./L/Led Zeppelin - Discography/Led Zeppelin - Led Zeppelin III/AlbumArt_{C9AAD432-BF70-4AFB-97C6-14734CD290B8}_Large.jpg
./L/Led Zeppelin - Discography/Led Zeppelin - Led Zeppelin III/Folder.jpg
like image 461
Dirk Avatar asked Jul 19 '14 20:07

Dirk


1 Answers

Use the pipes.quote function. In Python 3 this function was renamed to shlex.quote. These functions quote a string so that it can safely be used as part of a shell command.

like image 72
pdw Avatar answered Sep 29 '22 13:09

pdw