Beginner question: I am trying to rename all .xlsx files within a directory. I understand how to replace a character in string with another, but how about removing? More specifically, I have multiple files in a directory: 0123_TEST_01, 0456_TEST_02. etc. I am trying to remove the prefix in the file name, which would result in the following: TEST_01, TEST_02.
初学者问题:我正在尝试重命名目录中的所有.xlsx文件。我理解如何将字符串中的字符替换为另一个字符,但如何删除?更具体地说,我在目录中有多个文件:0123_TEST_01,0456_TEST_02。我试图删除文件名中的前缀,这将导致以下结果:TEST_01,TEST_02。
I am attempting to use os.rename and throw it into a loop, but am unsure if I should use len() and some math to try and return the correct naming convention. The below code is where I currently stand. Please let me know if this does not make sense. Thanks.
我试图使用os.rename并将其抛入循环,但我不确定是否应该使用len()和一些数学来尝试返回正确的命名约定。以下代码是我目前所处的位置。如果这没有意义,请告诉我。谢谢。
import os
import shutil
import glob
src_files = os.listdir('C:/Users/acars/Desktop/b')
for file_name in src_files:
os.rename(fileName, filename.replace())
2 个解决方案
#1
1
Just split once on an underscore and use the second element, glob will also find all your xlsx
file for you are return the full path:
只需在下划线上拆分一次并使用第二个元素,glob也将为您找到所有xlsx文件返回完整路径:
from os import path, rename
from glob import glob
src_files = glob('C:/Users/acars/Desktop/b/*.xlsx')
pth = 'C:/Users/acars/Desktop/b/'
for file_name in src_files:
rename(file_name, path.join(pth, path.basename(file_name).split("_",1)[1])
If you only have xlsx files and you did not use glob you would need to join the paths:
如果您只有xlsx文件且未使用glob,则需要加入路径:
from os import path, rename
from glob import glob
pth = 'C:/Users/acars/Desktop/b'
src_files = os.listdir(pth)
for file_name in src_files:
new = file_name.split("_", 1)[1]
file_name = path.join(pth, file_name)
rename(file_name, path.join(pth, new))
#2
1
Just split the file name by underscore, ignore the first part, and join it back again.
只需按下划线拆分文件名,忽略第一部分,然后再将其加入。
>>> file_name = '0123_TEST_01'
>>> '_'.join(file_name.split('_')[1:])
'TEST_01'
Your code will look like this:
您的代码将如下所示:
for file_name in src_files:
os.rename(file_name, '_'.join(file_name.split('_')[1:]))
#1
1
Just split once on an underscore and use the second element, glob will also find all your xlsx
file for you are return the full path:
只需在下划线上拆分一次并使用第二个元素,glob也将为您找到所有xlsx文件返回完整路径:
from os import path, rename
from glob import glob
src_files = glob('C:/Users/acars/Desktop/b/*.xlsx')
pth = 'C:/Users/acars/Desktop/b/'
for file_name in src_files:
rename(file_name, path.join(pth, path.basename(file_name).split("_",1)[1])
If you only have xlsx files and you did not use glob you would need to join the paths:
如果您只有xlsx文件且未使用glob,则需要加入路径:
from os import path, rename
from glob import glob
pth = 'C:/Users/acars/Desktop/b'
src_files = os.listdir(pth)
for file_name in src_files:
new = file_name.split("_", 1)[1]
file_name = path.join(pth, file_name)
rename(file_name, path.join(pth, new))
#2
1
Just split the file name by underscore, ignore the first part, and join it back again.
只需按下划线拆分文件名,忽略第一部分,然后再将其加入。
>>> file_name = '0123_TEST_01'
>>> '_'.join(file_name.split('_')[1:])
'TEST_01'
Your code will look like this:
您的代码将如下所示:
for file_name in src_files:
os.rename(file_name, '_'.join(file_name.split('_')[1:]))