Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何循环进程_Python_Loops_File Organization - Fatal编程技术网

Python:如何循环进程

Python:如何循环进程,python,loops,file-organization,Python,Loops,File Organization,我正在制作一个小应用程序/脚本,用于按照我想要的方式将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.getTra

我正在制作一个小应用程序/脚本,用于按照我想要的方式将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多行代码,这些代码非常简单。所以我想知道是否还有其他方法可以使用循环。然而,使用循环的问题是,对于每个块,我必须更改列表中的数字。例如,从块1到块2,我必须将
musfile[0]
更改为
musfile[1]
,我不知道如何使用循环来实现这一点。事实上,我对循环知之甚少。我够清楚了吗

这并没有回答您最初的问题,但这是我对类似问题的解决方案:

这并没有回答您最初的问题,但这是我对类似问题的解决方案:

对不起,不起作用,因为循环中的第二个tim,它为makedirs返回一个错误,因为目录已经创建。@user738534在其周围添加一个try:except:语句抱歉,不起作用,因为循环中的第二个tim,它为makedirs返回一个错误,因为目录已经创建。@user738534在其周围添加一个try:except:语句
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)