1. 程式人生 > >Python學習練習:批量移動檔案

Python學習練習:批量移動檔案

今天想往MP3下點音樂,但是滿了,想把裡面不喜歡的刪了,但是音樂都在各個資料夾下,於是,我希望所有的音樂檔案可以移動到一個層面,以供我按照音樂專輯的封面來挑選要刪哪個,於是就想寫個程式來做一下整理,想想用我擅長的Java來寫吧,但是沒什麼意思,想到一直想學Python,何不拿Python練練手呢?

於是:安裝環境:2.5,3.4,2.7,我有3個版本,在練習的時候發現有些語法問題,我猜可能是版本的問題,於是換了3個版本,最後用了最穩定的2.7。IDE使用的是Eclipse。

'''
@author: Sahadev
'''
import os
import shutil

path = "H:\Music\CloudMusic";
listdir1 = os.listdir(path)
i = len(listdir1)
print i
for j in range(i):
    print(listdir1[j])
    file = path + '\\' + listdir1[j]
    isdir = os.path.isdir(file)
    if isdir:
        childPath = os.listdir(file)
        len1 = len(childPath)
        for k in range(len1):
            result = file + '\\' + childPath[k]
            childFileName = path + '\\' + childPath[k]
            print childFileName
            os.rename(result, childFileName)
        if len1 == 0:
            os.removedirs(file)

剛開始寫的程式,貼出來也沒有什麼註釋,可能程式碼只有我自己知道了,嗯,就是個見證。

再貼上控制檯日誌:

158
Wavves
Waylon Jennings
X-Ray Dog
Yeasater
Yellowman
Zedd
上田雅美
天野正道,鈴木幸太,海田明裡
宗次郎
小林啟樹
小見山優子
山岡晃
川井憲次
本田晃弘
柴田徹也
植鬆伸夫
澤野弘之
石元丈晴
Alexey Omelchuk - The Farewell.mp3
Audio Machine - An Unfinished Life.mp3
Audio Machine - Breath and Life.mp3
Audio Machine - Danuvius.mp3
Audio Machine - Equinox (No Choir).mp3
Audio Machine - Shadowfall.mp3
Audio Machine - Sol Invictus.mp3
Audio Machine - The New Earth.mp3
Bill Brown,Mikael Sandgren - Generals Main Theme.mp3
Brand X Music - Eventide.mp3
Brian Reitzell - Ghost of the Past.mp3
Brian Tyler - Assassin's Creed IV Black Flag Main Theme.mp3
Brian Tyler - Falling Into A Dream.mp3
Brian Tyler - Mw3 End Credits.mp3
Brian Tyler - Need for Speed: The Run.mp3
Bryan Adams - Run Free.mp3
Chris Tilton - Mercenaries Main Theme.mp3
Dirk Reichardt - Weekend.mp3
Dreamtale - Intro: The Dawn.mp3
E.S. Posthumus - Nara.mp3
Elitsa Alexandrova - Assassin's Creed Rogue Main Theme.mp3
Epic Score - We Owe You Our Lives.mp3
Era - Prayer.mp3
Gerard Marino - God Of War Iii Overture.mp3
Glenn Stafford - Heart of the Swarm.mp3
Gothic Storm - Stand And Fight.mp3
Greg Edmonson - Nate's Theme 3.0.mp3
Gregorian - O Fortuna (Single Edit).mp3
Gustaf Grefberg - Mayhem Dressed in a Suit.mp3
Hans Zimmer - Antrozous.mp3
Hans Zimmer - Crysis 2 Intro - intro.mp3
Hans Zimmer - End Credits.mp3
Hans Zimmer - Opening Credits.mp3
Hans Zimmer - Rock House Jail.mp3
Hans Zimmer - The Enemy Of My Enemy Is My Friend.mp3
Hans Zimmer,Lorne Balfe - Jodie's Suite.mp3
Harry Gregson-Williams - Life.mp3
Harry Gregson-Williams - Path to Heaven.mp3
Health - Tears.mp3
Henry Jackman - First Class.mp3
Ian Livingstone - The Month Of Valiant Effort.mp3
Imagine Dragons - Warriors.mp3
Immediate Music - Dark Side Of Power.mp3
Immediate Music - Glory Seeker.mp3
Immediate Music - Serenata Immortale.mp3
Immediate Music - The Black Legend.mp3
Inon Zur - Main Title.mp3
Jack Wall - Adrenaline.mp3
Jai Paul - Jasmine.mp3
James Hannigan - Ra3 Theme - Soviet March.mp3
James Hannigan - To Save Humanity.mp3
Jamie Christopherson - Main Theme ~LOST PLANET~.mp3
Jason Graves - Convergence Delayed.mp3
Jeff van Dyck - Forever (ROME- TOTAL WAR).mp3
Jeremy Soule - Dragonborn (1).mp3
Jeremy Soule - Dragonborn.mp3
Jesper Kyd - Death Brings Hope.mp3
Jesper Kyd - Ezio's Family.mp3
Jesper Kyd - Hitman 2 Main Title.mp3
Jesper Kyd - Three Horns Valey Boom Boom Fight.mp3
Johan Skugge,Jukka Rintamaki - Battlefield 3 Main Theme.mp3
John Dreamer - End of My Journey.mp3
Joseph Trapanese - Introductions.mp3
Kevin McMullan - Noddinagushpa (Main Title).mp3
KOKIA - Believe in the Spirit.mp3
KOKIA - EXEC_COSMOFLIPS/.mp3
League of Legends - Challengers.mp3
League of Legends - Welcome to Planet Urf.mp3
London Philharmonic Orchestra - Legend of Zelda: Suite.mp3
Malcolm Kirby Jr. - Saints Row IV.mp3
Martin O'Donnell - Halo Theme Mjolnir Mix.mp3
Matt Uelmen - Wilderness.mp3
Mike Zarin - Welcome to 4th Echelon.mp3
Motoi Sakuraba - Velstadt the Royal Aegis.mp3
Neil Davidge - Arrival.mp3
Nick Arundel - Arkham City Main Theme.mp3
Noisia - Mundus Theme.mp3
Poets of the Fall - The Poet & The Muse.mp3
Ramin Djawadi - Deploy.mp3
Ramin Djawadi - Main Title.mp3
Ramin Djawadi - Pacific Rim.mp3
Russell Brower - And The Heavens Shall Tremble.mp3
Russell Brower - I Am Justice.mp3
Russell Brower - The Wandering Isle.mp3
Russell Brower - Wrath Of The Lich King (Main Title).mp3
Russell Brower,Neal Acree - Times Change.mp3
Sascha Dikiciyan - Alex Theme.mp3
Seo Jeong Min - Counter Strike Online.mp3
Soundtrack - Arthas, My Son (Cinematic Intro) - intro.mp3
Soundtrack - Baba Yet (Menu music).mp3
Soundtrack - Crysis 3 - New York Memories.mp3
Soundtrack - Hell March 3.mp3
Soundtrack - Main Theme.mp3
Soundtrack - Main Titles.mp3
Soundtrack - Menu Opening-DarkNightEnvoy.mp3
Soundtrack - Wings Of Liberty.mp3
Stephen Barton - Wallrunner.mp3
Steve Jablonsky - Armored Prayer.mp3
Steve Jablonsky - Forgotten Sands (Main Theme).mp3
Steven Burke - Hero's Theme.mp3
Thomas Bergersen - Empire of Angels.mp3
Thomas Bergersen - Homecoming.mp3
Tracy Bush - Main Title_Legends of Azeroth.mp3
Tracy Bush - Stormwind(City Theme).mp3
Two Steps From Hell - Blackheart.mp3
Two Steps From Hell - Dragon Rider74.mp3
Two Steps From Hell - Heart of Courage - Original.mp3
Two Steps From Hell - Strength Of A Thousand Men.mp3
Two Steps From Hell - Victory.mp3
V.A. - A Hero Unmasked.mp3
V.A. - Main Theme.mp3
V.A. - Monster Hunter 4.mp3
V.A. - ベヨネッタ戦闘態勢 A (EV03-7).mp3
Vangelis - Titans.mp3
Various Artists - hero select underscore loop.mp3
Various Artists - select intl.mp3
Wavves - Nine Is God.mp3
Waylon Jennings - I Ain't Living Long Like This.mp3
X-Ray Dog - String Tek.mp3
Yeasater - Don't Come Close.mp3
Yellowman - Nobody Move;Nobody Gets Hurt.mp3
Zedd - The Legend Of Zelda - Original Mix.mp3
上田雅美 - The Last Escape.mp3
天野正道,鈴木幸太,海田明裡 - Onimusha 3 Opening.mp3
宗次郎 - 故鄉的原風景.mp3
小林啟樹 - Zero.mp3
小見山優子 - 英雄の証3(tri-)Version.mp3
山岡晃 - Theme of Laura.mp3
川井憲次 - 七劍戰歌.mp3
本田晃弘 - METAL GEAR SOLID PEACE WALKER Main Theme.mp3
柴田徹也 - The Time Has Come.mp3
植鬆伸夫 - Answers.mp3
澤野弘之 - Ace and Arms.mp3
澤野弘之 - CODENAMEZ.mp3
澤野弘之 - THEMEX.mp3
石元丈晴 - 世に仇なす者 (FFVII『片翼の天使』より).mp3
石元丈晴 - 我ら來たれり.mp3

OK,Python也算是體驗了一把了。