Python:如何循环进程

发布于 2024-11-04 22:31:31 字数 1117 浏览 0 评论 0原文

我正在制作一个小型应用程序/脚本,以便按照我想要的方式将我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者 2024-11-11 22:31:31
import os, os.path
import shutil

for m in musfile:
    tag.link(m)
    mar = str(tag.getArtist())
    mal = str(tag.getAlbum())
    mti = str(tag.getTitle())
    new_name = mar + ' - ' + mti + '.mp3'
    os.rename(m, new_name)
    new_dir = os.path.join(newmusicdir, mar, mal)    #use os.path.join instead of +'/' to be more multi platform, it is a good habit
    try:
        os.makedirs(new_dir)
    except:
        pass
    shutil.copy(new_name, new_dir)
import os, os.path
import shutil

for m in musfile:
    tag.link(m)
    mar = str(tag.getArtist())
    mal = str(tag.getAlbum())
    mti = str(tag.getTitle())
    new_name = mar + ' - ' + mti + '.mp3'
    os.rename(m, new_name)
    new_dir = os.path.join(newmusicdir, mar, mal)    #use os.path.join instead of +'/' to be more multi platform, it is a good habit
    try:
        os.makedirs(new_dir)
    except:
        pass
    shutil.copy(new_name, new_dir)
ˉ厌 2024-11-11 22:31:31

这并没有回答您原来的问题,但这是我对类似问题的解决方案: 进口商

This doesn't answer your original question, but here's my solution to a similar problem: Importer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文