如何重命名所有文件以包含目录名称?
How to rename all files to include the directory name?
我正在尝试在下面的代码中使用 For 循环来遍历文件列表并使用文件目录的名称重命名它们。
import re # add this to your other imports
import os
for files in os.walk("."):
for f_new in files:
folder = files.split(os.sep)[-2]
print(folder)
name_elements = re.findall(r'(Position)(\d+)', f_new)[0]
name = name_elements[0] + str(int(name_elements[1]))
print(name) # just for demonstration
dst = folder + '_' + name
print(dst)
os.rename('Position014 (RGB rendering) - 1024 x 1024 x 1 x 1 - 3 ch (8 bits).tif', dst)
使用pathlib
Path.rglob
: This is like calling Path.glob()
,在给定的相关模式前添加 '**/'
:
.parent
or .parents[0]
:一个不可变的序列,提供对路径的逻辑祖先的访问
- 如果你想要路径的不同部分,索引
parents[]
不同
file.parents[0].stem
returns 'test1'
或 'test2'
取决于文件
file.parents[1].stem
returns 'photos'
file.parents[2].stem
returns 'stack_overflow'
.stem
:最后的路径组件,不带后缀
.suffix
: 最终组件的文件扩展名
.rename
: 将此文件或目录重命名为给定目标
- 以下代码仅查找
.tiff
个文件。使用 *.*
获取所有文件。
- 如果您只想要
file_name
的前 10 个字符:
file_name = file_name[:10]
form pathlib import Path
# set path to files
p = Path('e:/PythonProjects/stack_overflow/photos/')
# get all files in subdirectories with a tiff extension
files = list(p.rglob('*.tiff'))
# print files example
[WindowsPath('e:/PythonProjects/stack_overflow/photos/test1/test.tiff'), WindowsPath('e:/PythonProjects/stack_overflow/photos/test2/test.tiff')]
# iterate through files
for file in files:
file_path = file.parent # get only path
dir_name = file.parent.stem # get the directory name
file_name = file.stem # get the file name
suffix = file.suffix # get the file extension
file_name_new = f'{dir_name}_{file_name}{suffix}' # make the new file name
file.rename(file_path / file_name_new) # rename the file
# output files renamed
[WindowsPath('e:/PythonProjects/stack_overflow/photos/test1/test1_test.tiff'), WindowsPath('e:/PythonProjects/stack_overflow/photos/test2/test2_test.tiff')]
我正在尝试在下面的代码中使用 For 循环来遍历文件列表并使用文件目录的名称重命名它们。
import re # add this to your other imports
import os
for files in os.walk("."):
for f_new in files:
folder = files.split(os.sep)[-2]
print(folder)
name_elements = re.findall(r'(Position)(\d+)', f_new)[0]
name = name_elements[0] + str(int(name_elements[1]))
print(name) # just for demonstration
dst = folder + '_' + name
print(dst)
os.rename('Position014 (RGB rendering) - 1024 x 1024 x 1 x 1 - 3 ch (8 bits).tif', dst)
使用pathlib
Path.rglob
: This is like callingPath.glob()
,在给定的相关模式前添加'**/'
:.parent
or.parents[0]
:一个不可变的序列,提供对路径的逻辑祖先的访问- 如果你想要路径的不同部分,索引
parents[]
不同file.parents[0].stem
returns'test1'
或'test2'
取决于文件file.parents[1].stem
returns'photos'
file.parents[2].stem
returns'stack_overflow'
- 如果你想要路径的不同部分,索引
.stem
:最后的路径组件,不带后缀.suffix
: 最终组件的文件扩展名.rename
: 将此文件或目录重命名为给定目标- 以下代码仅查找
.tiff
个文件。使用*.*
获取所有文件。 - 如果您只想要
file_name
的前 10 个字符:file_name = file_name[:10]
form pathlib import Path
# set path to files
p = Path('e:/PythonProjects/stack_overflow/photos/')
# get all files in subdirectories with a tiff extension
files = list(p.rglob('*.tiff'))
# print files example
[WindowsPath('e:/PythonProjects/stack_overflow/photos/test1/test.tiff'), WindowsPath('e:/PythonProjects/stack_overflow/photos/test2/test.tiff')]
# iterate through files
for file in files:
file_path = file.parent # get only path
dir_name = file.parent.stem # get the directory name
file_name = file.stem # get the file name
suffix = file.suffix # get the file extension
file_name_new = f'{dir_name}_{file_name}{suffix}' # make the new file name
file.rename(file_path / file_name_new) # rename the file
# output files renamed
[WindowsPath('e:/PythonProjects/stack_overflow/photos/test1/test1_test.tiff'), WindowsPath('e:/PythonProjects/stack_overflow/photos/test2/test2_test.tiff')]