使用python自动移动文件

使用python自动移动文件,python,linux,python-2.7,python-3.x,ubuntu,Python,Linux,Python 2.7,Python 3.x,Ubuntu,我正在尝试制作一个python脚本,它允许我将音乐放入一个大文件夹中,因此当我运行脚本时,它将根据音乐文件的第一部分创建文件夹。假设我有一个名为OMFG-Ice Cream.mp3的音乐文件,我希望能够分割每个音乐文件名,这样,在这种情况下,它将把它切掉Ice Cream.mp3,然后使用OMFG创建一个名为该文件的文件夹。在它创建了这个文件夹之后,我想找到一种方法,然后移动到刚刚创建的文件夹中,在本例中是OMFG-Ice Cream.mp3 以下是我目前的代码: 导入操作系统 #path=“/

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

以下是我目前的代码:

导入操作系统
#path=“/Users/alowe/Desktop/testdir2”
#os.listdir(路径)
歌曲列表=['OMFG-icecream.mp3','OMFG-Hello.mp3','Dillistone-Sad&High.mp3']
teststr=str(歌曲列表)
songs=teststr.partition('-')[0]
打印“”。加入(歌曲)[2:-1]
我的主要问题是如何循环遍历字符串中的每个对象

谢谢,
Alex

您可以尝试以下代码:

  • 循环浏览列表
  • 将每个元素拆分到列表中
  • 如果文件夹尚不存在,请创建该文件夹
  • 将音乐传送到该文件夹
循环浏览列表

    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()))
使用方便:

例如:

$ 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

嗯。。。你忘了转移什么东西了吗?比如说,使用
os.rename
shutil.move
?我承认提问者要求的是更简单的东西,这让我觉得他们真的需要运行一个合适的Python教程。我只是编写了伪代码,假设他/她已经知道要传输的函数。但好的,我可以编辑和添加文件传输。(舒蒂尔等)它看起来几乎完成了,所以省略最后一步似乎很奇怪。没什么大不了的。好吧,我只是为了你的健康而加上纱布completeness@ShadowRanger是的,我对Python相当陌生。那么你会推荐我做什么教程呢?你的脚本做得很好!!!但是有没有一种方法可以让我在每次键入命令时不必键入
/Users/alowe/Desktop/testdir2
?也许可以在Python脚本中添加文件夹路径?@Alex:是的,如果你想硬编码路径;把它放在脚本中,而不是
Path.home()/“Music”
。也不要把
OMFG-Ice Cream.mp3
移动到
OMFG/Ice Cream.mp3
我可以把它移动到
OMFG/OMFG-Ice Cream.mp3吗?如果有办法,我已经更新了答案。要保留旧名称,请使用
dst_dir/path.name
而不是
dst_dir/name.lstrip()
。此脚本不适用于
Ubuntu 14.04.3 LTS
,因为它只有
Python 3.4
我需要做哪些修改才能使此脚本适用于Python 3.4?
#!/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