Python 如何根据包含相同文件的目录结构重新排列文件?

Python 如何根据包含相同文件的目录结构重新排列文件?,python,linux,shell,backup,file-management,Python,Linux,Shell,Backup,File Management,我正在将计算机中的备份/同步文件保存到外部硬盘 例如,我在外部硬盘中对一些文件进行了如下排序 (我有大约1000个目录和10000个文件,下面给出的目录结构仅供说明) 在我的电脑里,我在“temp”文件夹中有相同的文件 我在哪里 我希望“temp”中的文件像这样排列 temp --folderA -aa.jpg -ab.mp3 -ac.mp4 --folderB -ba.jpg -bb.mp3 -bc.mp4 是否有任何工具或脚本可以为我执行

我正在将计算机中的备份/同步文件保存到外部硬盘

例如,我在外部硬盘中对一些文件进行了如下排序 (我有大约1000个目录和10000个文件,下面给出的目录结构仅供说明)

在我的电脑里,我在“temp”文件夹中有相同的文件

我在哪里

我希望“temp”中的文件像这样排列

temp

--folderA
    -aa.jpg
    -ab.mp3
    -ac.mp4

--folderB
    -ba.jpg
    -bb.mp3
    -bc.mp4

是否有任何工具或脚本可以为我执行此操作(1000多个目录和10000多个文件)?

对于Windows,请在python脚本下方创建并将其复制到临时目录,只需将路径替换为“临时”即可:

import os
import shutil

if not os.path.exists('C:/Users/myuser/Desktop/day/folderA'):
       os.makedirs('C:/Users/myuser/Desktop/day/folderA')
if not os.path.exists('C:/Users/myuser/Desktop/day/folderB'):
       os.makedirs('C:/Users/myuser/Desktop/day/folderB')

sourcepath='C:/Users/myuser/Desktop/day'
source = os.listdir(sourcepath)
destinationpath = 'C:/Users/myuser/Desktop/day/folderA'
destinationpath2 = 'C:/Users/myuser/Desktop/day/folderB'
for files in source:
if files.startswith('a'):
    shutil.move(os.path.join(sourcepath,files), os.path.join(destinationpath,files))
if files.startswith('b'):
    shutil.move(os.path.join(sourcepath,files), os.path.join(destinationpath2,files))

listA = os.listdir('C:/Users/myuser/Desktop/day/folderA')
listA.sort()
listB = os.listdir('C:/Users/myuser/Desktop/day/folderB')
listB.sort()
temp

--folderA
    -aa.jpg
    -ab.mp3
    -ac.mp4

--folderB
    -ba.jpg
    -bb.mp3
    -bc.mp4
import os
import shutil

if not os.path.exists('C:/Users/myuser/Desktop/day/folderA'):
       os.makedirs('C:/Users/myuser/Desktop/day/folderA')
if not os.path.exists('C:/Users/myuser/Desktop/day/folderB'):
       os.makedirs('C:/Users/myuser/Desktop/day/folderB')

sourcepath='C:/Users/myuser/Desktop/day'
source = os.listdir(sourcepath)
destinationpath = 'C:/Users/myuser/Desktop/day/folderA'
destinationpath2 = 'C:/Users/myuser/Desktop/day/folderB'
for files in source:
if files.startswith('a'):
    shutil.move(os.path.join(sourcepath,files), os.path.join(destinationpath,files))
if files.startswith('b'):
    shutil.move(os.path.join(sourcepath,files), os.path.join(destinationpath2,files))

listA = os.listdir('C:/Users/myuser/Desktop/day/folderA')
listA.sort()
listB = os.listdir('C:/Users/myuser/Desktop/day/folderB')
listB.sort()