我开始写一个脚本来从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不喜欢它因为如果它试图重命名目录下的文件它不会破坏路径中的通配符(我猜这就是问题所在)
两个问题-
- 我如何解决这个问题,从发生在这种情况下?
- 这是最python化的方法吗?还是我在这里失去了情节?
使用工作示例更新
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
你正在尝试使用一个不存在的目录(/testdir/test/h.txt)重命名路径,因此你会得到一个错误
低于1也行。
>>> os.rename(str('/testdir/*test/h.txt'), '/testdir/*test/g.txt')
>>>
但是,你不想要任何特殊字符,所以,我建议在对文件进行操作之前先从目录路径中删除特殊字符,这样,你就不会得到'no such file or dir'错误。