Python: 如何用下划线替换所有文件、文件夹和子文件夹名称中的空格?
Python: How to replace whitespaces by underscore in the name of ALL files, folders and subfolders?
我们如何替换给定父文件夹中文件夹、子文件夹和文件名称中的空格?
我最初尝试更换到 8 级的尝试如下。我相信有更好的方法。我的代码看起来很难看。
我们非常欢迎更好的解决方案。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
def replace_space_by_underscore(path):
"""Replace whitespace in filenames by underscore."""
import glob
import os
for infile in glob.glob(path):
new = infile.replace(" ", "_")
try:
new = new.replace(",", "_")
except:
pass
try:
new = new.replace("&", "_and_")
except:
pass
try:
new = new.replace("-", "_")
except:
pass
if infile != new:
print(infile, "==> ", new)
os.rename(infile, new)
if __name__ == "__main__":
try:
replace_space_by_underscore('*/*/*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*')
except:
replace_space_by_underscore('*')
你需要一个递归的解决方案。重命名当前目录下的所有文件;然后对于每个子目录(如果有),进入该子目录 X(os.chdir(X)
),再次调用相同的函数,然后返回父目录(os.chdir("..")
)。
您可以使用 os.walk
来动态更改迭代文件夹的名称:
import os
def replace(parent):
for path, folders, files in os.walk(parent):
for f in files:
os.rename(os.path.join(path, f), os.path.join(path, f.replace(' ', '_')))
for i in range(len(folders)):
new_name = folders[i].replace(' ', '_')
os.rename(os.path.join(path, folders[i]), os.path.join(path, new_name))
folders[i] = new_name
os.walk
从 parent
开始以自上而下的顺序迭代目录树。对于每个文件夹,它 returns 元组 (current path, list of files, list of folders
)。给定的文件夹列表可以改变,os.walk
将在迭代的以下步骤中使用改变的内容。
运行之前的文件夹:
.
├── new doc
└── sub folder
├── another folder
├── norename
└── space here
之后:
.
├── new_doc
└── sub_folder
├── another_folder
├── norename
└── space_here
按照@niemmi 的确切想法,我最终得到了这个:
警告:切勿从 HOME 目录或某些重要目录中 运行 此脚本,它会重命名所有文件,包括隐藏文件。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Date: Dec 15, 2016
def replace_space_by_underscore(parent):
"""Replace whitespace by underscore in all files and folders.
replaces , - [ ] () __ ==> underscore
"""
import os
for path, folders, files in os.walk(parent):
# rename the files
for f in files:
old = os.path.join(path, f)
bad_chars = [r' ', r',', r'-', r'&', r'[', r']', r'(', r')', r'__']
for bad_char in bad_chars:
if bad_char in f:
new = old.replace(bad_char, '_')
print(old, "==>", new)
os.rename(old, new)
# rename the folders
for i in range(len(folders)):
new_name = folders[i].replace(' ', '_')
bad_chars = [r' ', r',', r'-', r'&',
r'[', r']', r'(', r')', r'__']
for bad_char in bad_chars:
if bad_char in new_name:
new_name = new_name.replace(bad_char, '_')
print(folders[i], "==> ", new_name)
old = os.path.join(path, folders[i])
new = os.path.join(path, new_name)
os.rename(old, new)
folders[i] = new_name
if __name__ == "__main__":
replace_space_by_underscore('.')
我们如何替换给定父文件夹中文件夹、子文件夹和文件名称中的空格?
我最初尝试更换到 8 级的尝试如下。我相信有更好的方法。我的代码看起来很难看。 我们非常欢迎更好的解决方案。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
def replace_space_by_underscore(path):
"""Replace whitespace in filenames by underscore."""
import glob
import os
for infile in glob.glob(path):
new = infile.replace(" ", "_")
try:
new = new.replace(",", "_")
except:
pass
try:
new = new.replace("&", "_and_")
except:
pass
try:
new = new.replace("-", "_")
except:
pass
if infile != new:
print(infile, "==> ", new)
os.rename(infile, new)
if __name__ == "__main__":
try:
replace_space_by_underscore('*/*/*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*/*')
except:
pass
try:
replace_space_by_underscore('*/*')
except:
replace_space_by_underscore('*')
你需要一个递归的解决方案。重命名当前目录下的所有文件;然后对于每个子目录(如果有),进入该子目录 X(os.chdir(X)
),再次调用相同的函数,然后返回父目录(os.chdir("..")
)。
您可以使用 os.walk
来动态更改迭代文件夹的名称:
import os
def replace(parent):
for path, folders, files in os.walk(parent):
for f in files:
os.rename(os.path.join(path, f), os.path.join(path, f.replace(' ', '_')))
for i in range(len(folders)):
new_name = folders[i].replace(' ', '_')
os.rename(os.path.join(path, folders[i]), os.path.join(path, new_name))
folders[i] = new_name
os.walk
从 parent
开始以自上而下的顺序迭代目录树。对于每个文件夹,它 returns 元组 (current path, list of files, list of folders
)。给定的文件夹列表可以改变,os.walk
将在迭代的以下步骤中使用改变的内容。
运行之前的文件夹:
.
├── new doc
└── sub folder
├── another folder
├── norename
└── space here
之后:
.
├── new_doc
└── sub_folder
├── another_folder
├── norename
└── space_here
按照@niemmi 的确切想法,我最终得到了这个:
警告:切勿从 HOME 目录或某些重要目录中 运行 此脚本,它会重命名所有文件,包括隐藏文件。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Date: Dec 15, 2016
def replace_space_by_underscore(parent):
"""Replace whitespace by underscore in all files and folders.
replaces , - [ ] () __ ==> underscore
"""
import os
for path, folders, files in os.walk(parent):
# rename the files
for f in files:
old = os.path.join(path, f)
bad_chars = [r' ', r',', r'-', r'&', r'[', r']', r'(', r')', r'__']
for bad_char in bad_chars:
if bad_char in f:
new = old.replace(bad_char, '_')
print(old, "==>", new)
os.rename(old, new)
# rename the folders
for i in range(len(folders)):
new_name = folders[i].replace(' ', '_')
bad_chars = [r' ', r',', r'-', r'&',
r'[', r']', r'(', r')', r'__']
for bad_char in bad_chars:
if bad_char in new_name:
new_name = new_name.replace(bad_char, '_')
print(folders[i], "==> ", new_name)
old = os.path.join(path, folders[i])
new = os.path.join(path, new_name)
os.rename(old, new)
folders[i] = new_name
if __name__ == "__main__":
replace_space_by_underscore('.')