如何替换字符和重命名多个文件?
How to replace characters and rename multiple files?
我有一堆pdf文件,文件名如下:
- AuthorA_2014_这篇文章不错
- BIsanotherAuthor_1994_ Gr8 文章
- CIsFatherOfB_1994_Minor文章还不错
等等。我想将文件名更改为这种格式:
- 作者A2014这篇文章不错
- BIsanotherAuthor1994Gr8 文章
- CIsFatherOfB1994 次要文章但还不错
如何在 python 中执行此操作?我在 python 方面确实有初学者水平的知识。我尝试使用取自 here
的代码
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", ""))
使用此代码,我可以将标题从 AuthorA_2014_ This is a good article 更改为 AuthorA2014 This is a good article,它删除了下划线,但我不希望年份和标题之间有任何空格的文章。我该如何实现?
我正在使用 Python 3.7.7
这应该可以完成:
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", "").replace("_ ", ""))
您可以使用正则表达式删除带有可选尾随 space 的 _
:
import re
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, re.sub(r'_ ?', '', filename))
import re
import os
path = os.getcwd()
files = os.listdir(path)
for file in files:
os.rename(file, re.sub(r'_ ?', '', file))
使用pathlib:
- 此模块提供 类 表示文件系统路径的语义适用于不同的操作系统。
- 给定一个使用
t = Path.cwd() / 'test_foo_ bar.txt'
创建的 pathlib 对象
.rglob
找到所有 .pdf
个文件
WindowsPath('E:/PythonProjects/stack_overflow/test_foo_ bar.txt')
t.stem
是 'test_foo_ bar'
t.suffix
是 '.txt'
t.parent
是 WindowsPath('E:/PythonProjects/stack_overflow')
t.parent / 'new_name.txt'
是 WindowsPath('E:/PythonProjects/stack_overflow/new_name.txt')
t.rename(...)
重命名文件
from pathlib import Path
p = Path.cwd() # for current working directory or Path('/some_path/files')
for file in p.rglob('*.pdf'): # get all pdfs in all subdirectories
new_file_name = file.stem.replace('_', '').replace('_ ', '') + file.suffix
file.rename(file.parent / new_file_name)
我有一堆pdf文件,文件名如下:
- AuthorA_2014_这篇文章不错
- BIsanotherAuthor_1994_ Gr8 文章
- CIsFatherOfB_1994_Minor文章还不错
等等。我想将文件名更改为这种格式:
- 作者A2014这篇文章不错
- BIsanotherAuthor1994Gr8 文章
- CIsFatherOfB1994 次要文章但还不错
如何在 python 中执行此操作?我在 python 方面确实有初学者水平的知识。我尝试使用取自 here
的代码import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", ""))
使用此代码,我可以将标题从 AuthorA_2014_ This is a good article 更改为 AuthorA2014 This is a good article,它删除了下划线,但我不希望年份和标题之间有任何空格的文章。我该如何实现?
我正在使用 Python 3.7.7
这应该可以完成:
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("_", "").replace("_ ", ""))
您可以使用正则表达式删除带有可选尾随 space 的 _
:
import re
import os
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, re.sub(r'_ ?', '', filename))
import re
import os
path = os.getcwd()
files = os.listdir(path)
for file in files:
os.rename(file, re.sub(r'_ ?', '', file))
使用pathlib:
- 此模块提供 类 表示文件系统路径的语义适用于不同的操作系统。
- 给定一个使用
t = Path.cwd() / 'test_foo_ bar.txt'
创建的 pathlib 对象.rglob
找到所有.pdf
个文件WindowsPath('E:/PythonProjects/stack_overflow/test_foo_ bar.txt')
t.stem
是'test_foo_ bar'
t.suffix
是'.txt'
t.parent
是WindowsPath('E:/PythonProjects/stack_overflow')
t.parent / 'new_name.txt'
是WindowsPath('E:/PythonProjects/stack_overflow/new_name.txt')
t.rename(...)
重命名文件
from pathlib import Path
p = Path.cwd() # for current working directory or Path('/some_path/files')
for file in p.rglob('*.pdf'): # get all pdfs in all subdirectories
new_file_name = file.stem.replace('_', '').replace('_ ', '') + file.suffix
file.rename(file.parent / new_file_name)