Python学习练习:批量移动文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011064099/article/details/50557237

今天想往MP3下点音乐,但是满了,想把里面不喜欢的删了,但是音乐都在各个文件夹下,于是,我希望所有的音乐文件可以移动到一个层面,以供我按照音乐专辑的封面来挑选要删哪个,于是就想写个程序来做一下整理,想想用我擅长的Java来写吧,但是没什么意思,想到一直想学Python,何不拿Python练练手呢?

于是:安装环境:2.5,3.4,2.7,我有3个版本,在练习的时候发现有些语法问题,我猜可能是版本的问题,于是换了3个版本,最后用了最稳定的2.7。IDE使用的是Eclipse。

通过查看简单的Python语法说明:http://pythonprogramminglanguage.com/以及一个API文档https://docs.python.org/2/library/index.html,勉勉强强的把这个程序写出来了:

'''
@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也算是体验了一把了。


阅读更多
换一批

没有更多推荐了,返回首页