文件夹重命名

天涯浪子 提交于 2020-02-17 19:03:14
import os,timepath = r"D:\重命名图片"# files =  os.listdir(path)list_url = ["Mainpage","New Products All products(Most Popular)","New Products All products(Featured)","New Products All products(Most Recent)","New Products Dresses(Most Popular)","New Products Dresses(Featured)","New Products Dresses(Most Recent)","New Products Bottoms(Most Popular)","New Products Bottoms(Featured)","New Products Bottoms(Most Recent)","New Products Outwear(Most Popular)","New Products Outwear(Featured)","New Products Outwear(Most Recent)","New Products Rompers Jumpsuits(Most Popular)","New Products Rompers Jumpsuits(Featured)","New Products Rompers Jumpsuits(Most Recent)","New Products Swim(Most Popular)","New Products Swim(Featured)","New Products Swim(Most Recent)","New Products Tops(Most Popular)","New Products Tops(Featured)","New Products Tops(Most Recent)","Back In Stock All products(Most Popular)","Back In Stock All products(Featured)","Back In Stock All products(Most Recent)","Back In Stock Dresses(Most Popular)","Back In Stock Dresses(Featured)","Back In Stock Dresses(Most Recent)","Back In Stock Bottoms(Most Popular)","Back In Stock Bottoms(Featured)","Back In Stock Bottoms(Most Recent)","Back In Stock Outwear(Most Popular)","Back In Stock Outwear(Featured)","Back In Stock Outwear(Most Recent)","Back In Stock Rompers Jumpsuits(Most Popular)","Back In Stock Rompers Jumpsuits(Featured)","Back In Stock Rompers Jumpsuits(Most Recent)","Back In Stock Swim(Most Popular)","Back In Stock Swim(Featured)","Back In Stock Swim(Most Recent)","Back In Stock Tops(Most Popular)","Back In Stock Tops(Featured)","Back In Stock Tops(Most Recent)","Top Rated All products(Most Popular)","Top Rated All products(Featured)","Top Rated All products(Most Recent)","Top Rated All products(Top Rated)","Top Rated Dresses(Most Popular)","Top Rated Dresses(Featured)","Top Rated Dresses(Most Recent)","Top Rated Dresses(Top Rated)","Top Rated Bottoms(Most Popular)","Top Rated Bottoms(Featured)","Top Rated Bottoms(Most Recent)","Top Rated Bottoms(Top Rated)","Top Rated Outwear(Most Popular)","Top Rated Outwear(Featured)","Top Rated Outwear(Most Recent)","Top Rated Outwear(Top Rated)","Top Rated Rompers Jumpsuits(Most Popular)","Top Rated Rompers Jumpsuits(Featured)","Top Rated Rompers Jumpsuits(Most Recent)","Top Rated Rompers Jumpsuits(Top Rated)","Top Rated Swim(Most Popular)","Top Rated Swim(Featured)","Top Rated Swim(Most Recent)","Top Rated Swim(Top Rated)","Top Rated Tops(Most Popular)","Top Rated Tops(Featured)","Top Rated Tops(Most Recent)","Top Rated Tops(Top Rated)","All Dress Most Popular","All Dress Featured","All Dress Most Recent","All Dress Top Rated","Formal Dress Most Popular","Formal Dress Featured","Formal Dress Most Recent","Formal Dress Top Rated","Clothing Tops(Most Popular)","Clothing Tops(Featured)","Clothing Tops(Most Recent)","Clothing Tops(Top Rated)","Clothing Bottoms(Most Popular)","Clothing Bottoms(Featured)","Clothing Bottoms(Most Recent)","Clothing Bottoms(Top Rated)","Clothing Rompers Jumpsuits(Most Popular)","Clothing Rompers Jumpsuits(Featured)","Clothing Rompers Jumpsuits(Most Recent)","Clothing Rompers Jumpsuits(Top Rated)","Clothing Demin(Most Popular)","Clothing Demin(Featured)","Clothing Demin(Most Recent)","Clothing Demin(Top Rated)","Clothing Two Piece Sets(Most Popular)","Clothing Two Piece Sets(Featured)","Clothing Two Piece Sets(Most Recent)","Clothing Two Piece Sets(Top Rated)","Clothing Outwear(Most Popular)","Clothing Outwear(Featured)","Clothing Outwear(Most Recent)","Clothing Outwear(Top Rated)","Clothing Knitwear(Most Popular)","Clothing Knitwear(Featured)","Clothing Knitwear(Most Recent)","Clothing Knitwear(Top Rated)","Clothing Office-chic(Most Popular)","Clothing Office-chic(Featured)","Clothing Office-chic(Most Recent)","Clothing Office-chic(Top Rated)","Clothing Loungewear(Most Popular)","Clothing Loungewear(Featured)","Clothing Loungewear(Most Recent)",]def get_file_list(file_path):    """文件按时间排序,从小到大排序"""    dir_list = os.listdir(file_path)    if not dir_list:        return    else:        # 注意,这里使用lambda表达式,将文件按照最后修改时间顺序升序排列        dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(file_path, x)))        return dir_listprint(get_file_list( r"D:\重命名图片"))files = get_file_list(r"D:\重命名图片")print(files)# 获取文件修改时间# for i in range(len(files)):#     file_time = time.localtime(os.stat(r"D:\重命名图片\{}" .format(files[i])).st_mtime)##     print("{}的打印时间是:" .format(files[i]),time.strftime("%Y-%m-%d %H:%M:%S",file_time))for f in range(len(files)):    """按照时间顺序,修改文件名称"""    #调试代码的方法:关键地方打上print语句,判断这一步是不是执行成功    print(f)    if "screencapture" in files[f] and files[f].endswith(".png") or files[f].endswith(".jpg"):        print("原来的文件名字是:{}".format(files[f]))        #找到老的文件所在的位置        old_file=os.path.join(path,files[f])        print("old_file is {}".format(old_file))        #指定新文件的位置,如果没有使用这个方法,则新文件名生成在本项目的目录中        new_file=os.path.join(path,'{}.png' .format(list_url[f]) )        print("File will be renamed as:{}".format(new_file))        os.rename(old_file,new_file)        print("修改后的文件名是:{}".format(files[f]))print(len(list_url))import timeimport datetime# import os## file_time = time.localtime(os.stat(r"D:\重命名图片\首页.png").st_mtime)# print(file_time)# print(time.strftime("%Y-%m-%d %H:%M:%S",file_time))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!