python实现文件自动排序

完整代码如下


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# python3

import os
import re

path = "D:\data\filename\"   #文件夹所在位置
filenames = os.listdir(path)
n=0
for i in filenames:
    n = n + 1
    #清除文件命中的数字
    temp = re.sub('[0-9A-Z、]', '', i)
    newname = str(n)+ '、' + i
    # data = re.compile('[0-9A-Z、a-z]+').findall(i)
    data=re.compile('[0-9]').findall(i)
    if data:
        print("Numbers already exist in the file!!!")
        # 清除文件夹名中所有数字、字母、特殊符号
        os.rename(path + i, path + temp)
        # print(temp)
    else:
        print("Add serial number")
        # 文件夹自动排序(递增)
        os.rename(path + i, path + newname)
原文地址:https://www.cnblogs.com/dddjh/p/14991652.html