Python 有没有更好的方法来替换文件名中的多个空格?

Python 有没有更好的方法来替换文件名中的多个空格?,python,python-3.x,Python,Python 3.x,我在S.O和谷歌搜索过,但还没有找到一个我能马上理解的解决方案。 下面的代码就是我所拥有的 我在下面添加了一些评论,只是为了澄清s.o 我希望它有一个较短的签名,但仍然非常容易理解/可读 守则: def fix_filenames_in(folder): ''' Removes spaces from filenames within a folder ''' files = os.listdir(folder) for file_name in files:

我在S.O和谷歌搜索过,但还没有找到一个我能马上理解的解决方案。 下面的代码就是我所拥有的

我在下面添加了一些评论,只是为了澄清s.o 我希望它有一个较短的签名,但仍然非常容易理解/可读

守则:

def fix_filenames_in(folder):
    ''' Removes spaces from filenames within a folder '''
    files = os.listdir(folder)

    for file_name in files:
        new_name = ''
        if ' ' in file_name:

            # I could have used .replace() but decided to go with splitting on the space
            new_name = file_name.split(" ")

            # This filters empty splits in the list: ['file', '', 'name'] -> ['file', 'name']
            # Is there a better way to do this?
            new_name = [segment for segment in new_name if not segment == '']
            new_name = '-'.join(new_name)

            # Building file paths
            old_filename = folder + os.sep + file_name
            new_filename = folder + os.sep + new_name

            # Actual renaming
            os.rename(old_filename, new_filename)
运行它:

# Assuming the following list is from the os.listdir() call
images = ['bat  man.jpg', 'cat woman.jpg', 'the  scary  joker.png']

fix_filenames_in(images)

# Result:
['bat-man.jpg', 'cat-woman.jpg', 'the-scary-joker.png']

您可以使用str.split根据可能的多个空格进行标记化,然后使用带“-”字符的str.join再次连接回单个字符串

def fix_filenames_in(images):
    return ['-'.join(i.split()) for i in images]

>>> fix_filenames_in(images)
['bat-man.jpg', 'cat-woman.jpg', 'the-scary-joker.png']
正则表达式解决方案是使用带有模式r'\s+'的re.sub来匹配一个或多个空白字符

import re

def fix_filenames_in(images):
    return [re.sub(r'\s+', '-', i) for i in images]

>>> fix_filenames_in(images)
['bat-man.jpg', 'cat-woman.jpg', 'the-scary-joker.png']
您可以使用str.split根据可能的多个空格进行标记化,然后使用带“-”字符的str.join再次连接回单个字符串

def fix_filenames_in(images):
    return ['-'.join(i.split()) for i in images]

>>> fix_filenames_in(images)
['bat-man.jpg', 'cat-woman.jpg', 'the-scary-joker.png']
正则表达式解决方案是使用带有模式r'\s+'的re.sub来匹配一个或多个空白字符

import re

def fix_filenames_in(images):
    return [re.sub(r'\s+', '-', i) for i in images]

>>> fix_filenames_in(images)
['bat-man.jpg', 'cat-woman.jpg', 'the-scary-joker.png']
为什么不使用替换

这可能适合您:

file_name = file_name.replace(' ', '-')
<>编辑:我没有考虑有多个空间的情况。在这种情况下,您希望用单数“-”替换任意数量的序列空间。考虑:

import re

file_name = re.sub('\s+', '-', file_name)
\s表示查找空格,+表示至少查找一个。

为什么不使用replace

这可能适合您:

file_name = file_name.replace(' ', '-')
<>编辑:我没有考虑有多个空间的情况。在这种情况下,您希望用单数“-”替换任意数量的序列空间。考虑:

import re

file_name = re.sub('\s+', '-', file_name)

\s表示查找空格,+表示至少查找一个。

Regex:re.sub'\\s+','-',file_nameRegex:re.sub'\\s+','-',file_name谢谢,是的,我有。我使用.replace时遇到的问题是,如果文件名有两个空格字符,那么我将以两个破折号结束,而不是一个空格dash@BernardSolien你试过正则表达式吗?如果您正在寻找一些简单的东西,用regex替换将解决这个问题。编辑了我的答案以解决多个空格。谢谢,@Jace。我想下面是科里·克雷默的部分解决方案。我会稍微修改一下,做我想做的。谢谢你,是的,我已经做了。我使用.replace时遇到的问题是,如果文件名有两个空格字符,那么我将以两个破折号结束,而不是一个空格dash@BernardSolien你试过正则表达式吗?如果您正在寻找一些简单的东西,用regex替换将解决这个问题。编辑了我的答案以解决多个空格。谢谢,@Jace。我想下面是科里·克雷默的部分解决方案。我会稍微修改一下,做我想做的。谢谢你@Cory Kramer。函数实际上不返回任何内容,它只是重命名文件。但我非常喜欢这种方法。谢谢你@Cory Kramer。函数实际上不返回任何内容,它只是重命名文件。但我非常喜欢这种方法。