【问题标题】:escape whitespaces in linux path and file names转义 linux 路径和文件名中的空格
【发布时间】:2014-07-19 20:30:53
【问题描述】:

我实际上正在清理我的系统。和往常一样,我正在尝试以 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

【问题讨论】:

  • 我认为来自urllib 的东西可以解决问题。见related post
  • 你的意思是转义还是编码,例如%20?如果您的意思是转义,您在寻找什么转义字符?
  • 我希望将路径名作为另一个函数的参数传递,例如“commands.getstatusoutput”或“os.system”或来自子进程的东西。现在它不起作用,因为路径名有空格和其他特殊字符,所以我不能只将名称传递给某些子进程函数,因为 bash 不喜欢未转义的特殊字符。

标签: python regex file


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2011-11-27
    • 2013-08-01
    • 2015-07-18
    • 2012-05-09
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多