《Python編程快速上手》第9.8.3實踐練習

來源:互聯網
上載者:User

標籤:Python編程快速上手

#9.8.3 消除缺失的編號 """1.spam001.txt,spam002.txt,spam005.txt,如此。將前邊的消除,然後後邊逐一遞增2.空出一些編號,指定空出地區。然後修改編號。"""import re,os,shutil#find_path=input("輸入要尋找的目錄:")#find_path=os.path.abspath(find_path)find_path=os.path.abspath("D:\pytest")filenameReg=re.compile(r"(spam(\d+)\.txt$)")#將合格的檔案名稱添加為列表file_list=[]for name in os.listdir(find_path): reg_name=filenameReg.search(name) if reg_name: file_list.append(reg_name.group(1))#空餘值設計,不做過多類型及數值地區判斷,輸入地區符合正常範圍space=input("請確認是否留下空白區,以備後用(Y/N)")#預留配對修改名為字典chname_list={}if space.lower()==‘y‘: rang1=input("輸入空白區的起始值(包含此值為空白):") rang2=input("輸入空白區的結束值(包含此值為空白):") rang=range(int(rang1),int(rang2)+1) rang=list(rang) print("留空區為:"+str(rang)) #將需要處理的檔案名稱配對為字典 list1= file_list[:rang[0]-1].copy() for file in list1: print(list1.index(file)) filename="spam"+str(list1.index(file)+1).rjust(3,‘0‘)+".txt" chname_list[file]=filename list2= file_list[rang[0]-1:].copy() for file in list2: filename="spam"+str(list2.index(file)+rang[-1]+1).rjust(3,‘0‘)+".txt" chname_list[file]=filename elif space.lower()==‘n‘: #將需要處理的檔案名稱配對為字典 for file in file_list: filename="spam"+str(file_list.index(file)+1).rjust(3,‘0‘)+".txt" chname_list[file]=filename else: print("Input error") #對字典進行操作moveos.chdir(find_path)for k,v in chname_list.items(): shutil.move(k,v) print("move {} to {}".format(k,v))print("done")

《Python編程快速上手》第9.8.3實踐練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.