【问题标题】:Automatically move files with python使用python自动移动文件
【发布时间】:2015-10-20 03:35:55
【问题描述】:

我正在尝试制作一个 python 脚本,它允许我将音乐放入一个大文件夹中,因此当我运行脚本时,它将根据音乐文件的第一部分创建文件夹。因此,假设我有一个名为 OMFG - Ice Cream.mp3 的音乐文件,我希望能够拆分每个音乐文件名,以便在这种情况下将其切掉而不是 OMFG - Ice Cream.mp3 Ice Cream.mp3 然后它将使用 OMFG创建一个名为那个的文件夹。在它创建该文件夹后,我想找到一种移动方式,在本例中为 OMFG - Ice Cream.mp3 到刚刚创建的文件夹中。

到目前为止,这是我的代码:

import os

# path = "/Users/alowe/Desktop/testdir2"
# os.listdir(path)
songlist = ['OMFG - Ice Cream.mp3', 'OMFG - Hello.mp3', 'Dillistone - Sad & High.mp3']
teststr = str(songlist)
songs = teststr.partition('-')[0]
print ''.join(songs)[2:-1]

我的主要问题是如何遍历字符串中的每个对象。

谢谢, 亚历克斯

【问题讨论】:

    标签: python linux python-2.7 python-3.x ubuntu


    【解决方案1】:

    使用pathlib module for such tasks很方便:

    #!/usr/bin/env python3
    import sys
    from pathlib import Path
    
    src_dir = sys.argv[1] if len(sys.argv) > 1 else Path.home() / 'Music'
    for path in Path(src_dir).glob('*.mp3'): # list all mp3 files in source directory
        dst_dir, sep, name = path.name.partition('-')
        if sep: # move the mp3 file if the hyphen is present in the name
            dst_dir = path.parent / dst_dir.rstrip()
            dst_dir.mkdir(exist_ok=True) # create the leaf directory if necessary
            path.replace(dst_dir / name.lstrip()) # move file
    

    例子:

    $ python3.5 move-mp3.py /Users/alowe/Desktop/testdir2
    

    它将OMFG - Ice Cream.mp3 移动到OMFG/Ice Cream.mp3


    如果您想将OMFG - Ice Cream.mp3 移动到OMFG/OMFG - Ice Cream.mp3

    #!/usr/bin/env python3.5
    import sys
    from pathlib import Path
    
    src_dir = Path('/Users/alowe/Desktop/testdir2') # source directory
    for path in src_dir.glob('*.mp3'): # list all mp3 files in source directory
        if '-' in path.name: # move the mp3 file if the hyphen is present in the name
            dst_dir = src_dir / path.name.split('-', 1)[0].rstrip() # destination
            dst_dir.mkdir(exist_ok=True) # create the leaf directory if necessary
            path.replace(dst_dir / path.name) # move file
    

    【讨论】:

    • 你的脚本效果很好!!!但是有没有办法让我不必在每次输入命令时都输入/Users/alowe/Desktop/testdir2?也许通过在 Python 脚本中添加文件夹路径?
    • @Alex:是的,如果你想硬编码路径;把它放在脚本中而不是Path.home()/'Music'
    • 如果有办法的话,我可以将它移动到OMFG/Ice Cream.mp3,而不是将OMFG - Ice Cream.mp3移动到OMFG/Ice Cream.mp3吗?
    • @Alex:我已经更新了答案。要保留旧名称,请使用 dst_dir / path.name 而不是 dst_dir / name.lstrip()
    • 此脚本不适用于Ubuntu 14.04.3 LTS,因为它只有Python 3.4 需要进行哪些修改才能使此脚本与python 3.4 一起使用?
    【解决方案2】:

    您可以尝试以下代码:

    • 循环遍历列表
    • 将每个元素拆分到列表中
    • 如果文件夹不存在则创建文件夹
    • 将音乐传输到该文件夹​​

    循环遍历列表

        import os
        import shutil
        songlist = ['OMFG - Ice Cream.mp3', 'OMFG - Hello.mp3', 'Dillistone - Sad & High']
        m_dir = '/path/mainfolder'
        song_loc = '/path/songlocation'
    
        for song in songlist:
            s = song.split('-')
            if os.path.exists(os.path.join(m_dir,s[0])):
                shutil.copy(os.path.join(song_loc,song),os.path.join(m_dir,s[0].strip()))
            else:
                os.makedirs(os.path.join(m_dir,s[0]))
                shutil.copy(os.path.join(song_loc,song),os.path.join(m_dir,s[0].strip()))
    

    【讨论】:

    • 嗯...您忘记转移任何内容了吗?比如说os.rename 还是shutil.move?我承认提问者要求的东西要简单得多,这让我认为他们真的需要通过适当的 Python 教程来运行。
    • 我只是写了伪代码,假设他/她已经知道要转移的功能。但是好的,我可以编辑和添加文件传输。 (Shutil等)
    • 它看起来几乎完成了,所以省略最后一步似乎很奇怪。没什么大不了的。
    • 好吧,我只是为了完整起见添加shutil
    • @ShadowRanger 是的,我对 Python 还很陌生。那么你会推荐我做什么教程呢?
    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多