Python 递归地从文件夹/文件名中删除字符

Python 递归地从文件夹/文件名中删除字符,python,Python,我开始编写一个脚本,从linux操作系统漫游中删除非法字符。从文件开始,然后是文件夹。这是我到目前为止所拥有的- import sys import os import re # List of illegal chars to run through to replace in the filesystem walk chars = ['~', '*', '\\', ':', '<', '>', '|', '?', '"'] def ReplaceChars(value):

我开始编写一个脚本,从linux操作系统漫游中删除非法字符。从文件开始,然后是文件夹。这是我到目前为止所拥有的-

import sys
import os
import re

# List of illegal chars to run through to replace in the filesystem walk
chars = ['~', '*', '\\', ':', '<', '>', '|', '?', '"']

def ReplaceChars(value):
    for c in chars:
        value = value.replace(c, '')
    return value

def RenamePath(path):
    newFilePath = ReplaceChars(path)
    os.rename(path, newFilePath)

def WalkFileSystem(dirroot):
    # Main Walk Through File System
    for root, dirs, files in os.walk(dirroot, topdown=False):
        for name in files:
            searchObj = re.search(r'[%s]' % ''.join(chars), os.path.join(root, name))
            if searchObj:
                RenamePath(os.path.join(root, name))

        for name in dirs:
            searchObj = re.search(r'[%s]' % ''.join(chars), os.path.join(root, name))
            if searchObj:
                RenamePath(os.path.join(root, name))

if __name__ == "__main__":
    # Calls the WalkFileSystem Function
    WalkFileSystem('/TestFolder/')
它在某些情况下确实有效。问题是,如果我有一个像*test/os.rename这样的目录名,它不喜欢它,因为如果它试图重命名该目录下的文件,它不会破坏路径中的通配符,我想这就是问题所在

两个问题-

在这种情况下,我如何解决该问题? 这是做这件事的最疯狂的方式还是我失去了这里的情节? 使用工作示例更新

import argparse
import os
import re

# List of illegal chars to run through to replace in the filesystem walk
chars = ['~', '*', '\\', ':', '<', '>', '|', '?', '"']

def ReplaceChars(value):
    for c in chars:
        value = value.replace(c, '')
    return value


def RenamePath(root, path):
    newFilePath = ReplaceChars(path)
    os.rename(os.path.join(root, path), os.path.join(root, newFilePath))


def WalkFileSystem(dirroot):
    # Main Walk Through File System
    for root, dirs, files in os.walk(dirroot, topdown=False):

    for name in dirs:
        searchObj = re.search(r'[%s]' % ''.join(chars), name)
        if searchObj:
            RenamePath(root, name)


    for name in files:
        searchObj = re.search(r'[%s]' % ''.join(chars), name)
        if searchObj:
            RenamePath(root, name)


if __name__ == "__main__":
    # Calls the WalkFileSystem Function
    WalkFileSystem('/home/mpashby/Scripts/TestFolder/')

干杯,

这是因为,当脚本运行RenamePath时,它会执行以下操作:

>>> os.rename(str('/testdir/*test/h.txt'), '/testdir/test/h.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory

但是,您不需要任何特殊字符,因此,我建议在对文件执行此操作之前,首先从目录路径中删除特殊字符,这样,您就不会出现“没有这样的文件或目录”错误。

我最初确实想到了这一点,并决定先从下往上遍历路径。它仍然返回一个错误。能否在调用os.renamepath、newFilePath之前打印path、newFilePath并在引发异常之前共享它打印的内容?在这种情况下,您的建议是完全正确的。在os.rename调用中打印src和dst有助于我理解这个问题。查看路径,我可以看到我的脚本正在更改整个路径,包括dir是否包含任何不同的字符。这是在我的测试目录上抛出这些错误。用工作示例更新了问题。
>>> os.rename(str('/testdir/*test/h.txt'), '/testdir/*test/g.txt')
>>>