def getFile(prefixletter:str,ok:bool): """ 文件名添加后缀 :param prefixletter: :param ok: True 增加后缀,否则为删除 :return: """ try: nowfoloder = os.getcwd() curpath=os.path.join(nowfoloder,r"static") fiellist=os.listdir(curpath) n=1 os.chdir(curpath) for i in fiellist: print("list", i) if ok==False: newname=i.split("_") else: newname = i.split(".") print(newname[0]+'_'+prefixletter+"."+newname[1]) # if os.path.isfile(i): if ok==True: os.rename(i, newname[0] + '_' + prefixletter + "." + newname[1]) else: os.rename(i, newname[0] + ".text") n+=1 except Exception as ex: print(ex) finally: pass getFile("d", False) """ try: nowfoloder=os.getcwd() fillist=os.listdir(nowfoloder) print(fillist) curpath=os.path.join(nowfoloder,r"static") print(curpath) os.chdir(curpath) for f in os.listdir(): print(f) if(not os.path.isdir("images")): os.mkdir("images") if (not os.path.isdir("test")): os.mkdir("test") for f in os.listdir(): print(f) if os.path.isdir("test"): curpath = os.path.join(nowfoloder, r"static") os.chdir(curpath) print(os.getcwd()) if os.path.isdir("test"): os.rmdir(os.path.join(curpath,"test")) except Exception as ex: print(ex) finally: pass """
标签:python,curpath,nowfoloder,newname,File,print,path,os From: https://www.cnblogs.com/geovindu/p/17538659.html