python - 转义 linux 路径和文件名中的空格

标签 python regex file

我实际上是在清理我的系统。和往常一样,我试图用 python 的方式来做, 所以我正在清理音乐库中的重复项。现在我正试图找到一种模式 为 re 模块转义所有空格和特殊字符。但不幸的是我找不到有用的东西。这是我使用名为“fdupes”的 linux cli 工具生成的重复列表的一部分:

./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

最佳答案

使用 pipes.quote功能。在 Python 3 中,此函数被重命名为 shlex.quote .这些函数引用一个字符串,以便它可以安全地用作 shell 命令的一部分。

关于python - 转义 linux 路径和文件名中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844707/

相关文章:

python - 你如何刷新 Python 套接字?

c++ - regex_match 没有找到任何匹配项

python - 正则表达式 - 如何选择其中包含 '-' 的单词?

c - 使用两个 fprintf 语句写入 C 中的文件

c# - 在 C# 中,如何从域外访问域上的文件共享?

python - pickle Django 查询?

python - 如何从多个相同长度的列表中获取 numpy 数组并沿轴排序?

php - 如何从php中的文件夹中删除文件

Python/Django - 创建后编辑模型表单

java - 在java中使用正则表达式捕获重复的数字组