python学习笔记-批量添加OC文件前缀

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shengpeng3344/article/details/52994061

使用python2.7版本

def prefixFiles(path,prefix):
    #修改当前目录下全部文件的前缀 - 不包括子文件夹
    list = []
    files = os.listdir(path)  # 路径可以自己
    flag = True;
    for name in files:
        # print name;
        suffix = ['.m', '.cpp', '.h', '.mm']
        a = os.path.splitext(name)
        if a[1] in suffix:
            tmpStr = a[0]
            if tmpStr.startswith(prefix, 0, 4):#如果包含prefix
                pass
            else:
                newname = prefix + a[0] + a[1]
                if flag:
                    os.chdir(path)
                    flag = False
                tup = (os.path.join(path,newname),name,newname)
                list.append(tup)
                # os.rename(name, newname)

    return list


def prefixAllFiles(path,prefix):
    #修改当前目录及子目录下文件
    list = []
    list.extend(prefixFiles(path,prefix))
    for root, dirs, files in os.walk(path):
        list.extend(prefixFiles(root,prefix))
    return list

猜你喜欢

转载自blog.csdn.net/shengpeng3344/article/details/52994061
今日推荐