gpt4 book ai didi

python :Sort filenames in a directory and add a number in the beginning of each filename

转载 作者:行者123 更新时间:2023-12-01 09:30:56 28 4
gpt4 key购买 nike

1-intro-to deepleanring and computer visionk.MKV
2.Kaggle Deep Learning - YouTube.MP4
Convolutional Neural Networks - Fun and Easy Machine Learning - YouTube.MP4
Convolutional Neural Networks - The Math of Intelligence (Week 4)YouTube.MKV
Introduction to Deep Learning- What Are Convolutional Neural Networks- YouTube.MP4
Kaggle Deep Learning 3 - YouTube.MP4
Kaggle Deep Learning 4 - YouTube.MP4
Kaggle Deep Learning 5 Data Augmentation - YouTube.MP4
Kaggle Deep Learning 6 - YouTube.MP4
Kaggle Deep Learning 7.mp4
Kaggle Deep Learning 8 - YouTube.MP4

这些是需要排序的文件。包含数字的文件名(在本例中为 3)Kaggle Deep Learning 3 -YouTube.MP4 应重命名为3 Kaggle深度学习 - YouTube.MP4。不包含任何数字的文件名或开头包含数字的文件不需要重命名。

到目前为止我已经编写了以下代码,但我被卡住了

for f in os.listdir():
filename,extension = os.path.splitext(f))

简而言之,我希望这些文件在我的目录中看起来像这样..

1-intro-to deepleanring and computer visionk.MKV
2 Kaggle Deep Learning - YouTube.MP4
3 Kaggle Deep Learning - YouTube.MP4
4 Kaggle Deep Learning - YouTube.MP4
5 Kaggle Deep Learning Data Augmentation - YouTube.MP4
6 Kaggle Deep Learning - YouTube.MP4
7 Kaggle Deep Learning .mp4
8 Kaggle Deep Learning - YouTube.MP4
Convolutional Neural Networks - Fun and Easy Machine Learning - YouTube.MP4
Convolutional Neural Networks - The Math of Intelligence (Week 4)YouTube.MKV
Introduction to Deep Learning- What Are Convolutional Neural Networks-
YouTube.MP4

最佳答案

这不是一个干净的示例,但它可能会帮助您,在您想要重命名文件的文件夹中运行它。

警告:运行代码之前请备份您的内容...

import os

dirs = os.listdir(".")

#I am splitting every string into a chr because we dont know the location of the number
split_dir= [list(f) for f in dirs]


print "split_dir", split_dir

y = split_dir

for index, x in enumerate(split_dir):
for char in x:
print "char", char
if(char.isdigit()):
print "inside is digit"

y[index].remove(char)
y[index].insert(0,char)


y = [''.join(x) for x in y]
print "split_dir", y


#this section just renames the files in the current working directory
for index,file_name in enumerate(dirs):
print("yindex", y[index])
os.rename(file_name, y[index])

关于 python :Sort filenames in a directory and add a number in the beginning of each filename,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49968984/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com