class ChangeName(object): def getRandom(self): seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" sa = [] for i in range(8): sa.append(random.choice(seed)) salt = ''.join(sa) return salt def file_extension(self, path): return os.path.splitext(path)[1] def getSrcFileName(self, file_dir): for root, dirs, files in os.walk(file_dir): for file in files: # print file # 修改所有文件的名字 houzhui = self.file_extension(file) newname = self.getRandom() + houzhui srcpath = root newpath = srcpath.replace('src', 'tar') print srcpath + file print newpath + newname if not os.path.exists(newpath): os.mkdir(newpath) os.rename(srcpath + "/" + file, newpath + "/" + newname) def run(self): # 1.获取元目录所有的文件名 path = os.path.abspath(os.path.dirname(__file__)) self.getSrcFileName(path + "/src") if __name__ == "__main__": obj = ChangeName() obj.run()
python获取目录下所有的文件并修改文件名(随机8位字符窜名字)
猜你喜欢
转载自blog.csdn.net/qq_37792992/article/details/83624739
今日推荐
周排行