Python:如何循环进程
我正在制作一个小型应用程序/脚本,以便按照我想要的方式将我的 mp3 放入文件夹层次结构中,因为我还没有找到适合 Unix 的解决方案。所以我决定自己工作。这是摘录
if musfile[0]:
m = musfile[0]
tag.link(m)
mar = str(tag.getArtist())
mal = str(tag.getAlbum())
mti = str(tag.getTitle())
#m1track = str(tag.getTrack())
os.rename(m,mar + ' - ' + mti + '.mp3')
m = mar + ' - ' + mti + '.mp3'
os.makedirs(newmusicdir + '/' + mar + '/' + mal + '/')
shutil.copy(m,newmusicdir + '/' + mar + '/' + mal + '/')
if musfile[1]:
m = musfile[1]
tag.link(m)
mar = str(tag.getArtist())
mal = str(tag.getAlbum())
mti = str(tag.getTitle())
#m1track = str(tag.getTrack())
os.rename(m,mar + ' - ' + mti + '.mp3')
m = mar + ' - ' + mti + '.mp3'
os.makedirs(newmusicdir + '/' + mar + '/' + mal + '/')
shutil.copy(m,newmusicdir + '/' + mar + '/' + mal + '/')
等等。然而,为了组织多个文件,我只是重用了代码块。然而,这对于多个代码块来说效率极低。例如,如果我想用我的方法只组织 50 首歌曲,那么对于如此简单的事情,我将有超过 500 行代码。所以我想知道是否可以使用循环。然而,使用循环的问题是,对于每个块,我必须更改列表中的数字。例如,从块一到块二,我必须将 musfile[0]
更改为 musfile[1]
,但我不知道如何使用循环来做到这一点。事实上我对循环知之甚少。我说得够清楚吗?
I'm working on making a small app/script for putting my mp3's in a folder hierarchy the way I want it, as I haven't found a solution for Unix that is just quite right. So I decided to work on my own. This is an excerpt
if musfile[0]:
m = musfile[0]
tag.link(m)
mar = str(tag.getArtist())
mal = str(tag.getAlbum())
mti = str(tag.getTitle())
#m1track = str(tag.getTrack())
os.rename(m,mar + ' - ' + mti + '.mp3')
m = mar + ' - ' + mti + '.mp3'
os.makedirs(newmusicdir + '/' + mar + '/' + mal + '/')
shutil.copy(m,newmusicdir + '/' + mar + '/' + mal + '/')
if musfile[1]:
m = musfile[1]
tag.link(m)
mar = str(tag.getArtist())
mal = str(tag.getAlbum())
mti = str(tag.getTitle())
#m1track = str(tag.getTrack())
os.rename(m,mar + ' - ' + mti + '.mp3')
m = mar + ' - ' + mti + '.mp3'
os.makedirs(newmusicdir + '/' + mar + '/' + mal + '/')
shutil.copy(m,newmusicdir + '/' + mar + '/' + mal + '/')
And so on. However, in order to organize more than one file, I have been just reusing blocks of code. However, this is extremely inefficient for several blocks of code. For example, if I wanted to organize just 50 songs with my method, I would have over 500 lines of code, for something so simple. So I was wondering if there is anyway I could use loops. However, the problem with using loops is that with each block I must change the number in the list. For example, from block one to two, I must change musfile[0]
to musfile[1]
, and I do not know how to do that with loops. In fact I have little knowledge about loops. Am I clear enough?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并没有回答您原来的问题,但这是我对类似问题的解决方案: 进口商
This doesn't answer your original question, but here's my solution to a similar problem: Importer