首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python :对目录中的文件名进行排序,并在每个文件名的开头添加一个数字

Python :对目录中的文件名进行排序,并在每个文件名的开头添加一个数字
EN

Stack Overflow用户
提问于 2018-04-22 17:53:08
回答 2查看 129关注 0票数 1
代码语言:javascript
复制
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

这些文件需要对.A文件名进行排序,该文件名包含一个数字(在本例中为3),3 -YouTube.MP4应该重命名为3 YouTube.MP4。不包含任何数字的文件名或开头包含数字的文件不需要重命名。

到目前为止,我已经编写了下面的代码,而且我被困住了

代码语言:javascript
复制
for f in os.listdir():
    filename,extension = os.path.splitext(f))

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

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-23 08:31:13

最后,经过一番努力,我解决了这个问题,这个代码在很大程度上解决了我的问题。

将每个字符串(文件名)拆分成一个字符是关键。

代码语言:javascript
复制
import os
dirs = os.listdir(".")

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

#splitting every string into a char because we don't know the location of the number
filenames = list(filename)
for char in filenames:
    if(char.isdigit()):
        #print('digit is', char)
        filenames.remove(char)
        filenames.insert(0,char)


        name = ''.join(filenames)

        new_name = '{}{}'.format(name,extension)
        #print(new_name)
        os.rename(f,new_name)
票数 0
EN

Stack Overflow用户

发布于 2018-04-22 18:43:40

这不是一个简单的示例,但它可能会帮助您解决问题,请在要重命名文件的文件夹中运行此示例。

警告:在运行代码之前,一定要备份内容.

代码语言:javascript
复制
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])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49968984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档