python Sortr(基于模式的下载文件夹分拣机)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Sortr(基于模式的下载文件夹分拣机)相关的知识,希望对你有一定的参考价值。

{
  "defaultFolder": "/Downloads",
  "Patterns": {
    "Documents": {
      "extensions": [
        "*.docx",
        "*.pdf"
      ],
      "path": "/Documents"
    },
    "Pictures": {
      "extensions": [
        "*.png",
        "*.gif"
      ],
      "path": "/Pictures"
    },
    "Music": {
      "extensions": [
        "*.m4a",
        "*.mp3"
      ],
      "path": "/Music"
    },
    "Videos": {
      "extensions": [
        "*.mp4",
        "*.wmv"
      ],
      "path": "/Videos"
    }
  }
}
import json
from os import listdir,getenv
from os.path import isfile,join,isdir
import fnmatch,shutil

with open('./config.json') as config:
    d = json.load(config)

downloadsFolder = getenv('HOME')+d["defaultFolder"]
print("Using "+downloadsFolder+" as default Folder")
fileList = [f for f in listdir(downloadsFolder) if isfile(join(downloadsFolder, f))]
folderList = [f for f in listdir(downloadsFolder) if isdir(join(downloadsFolder, f))]

def moveFiles(src,dest):
    origSrc = join(downloadsFolder,src)
    print("Moving From - ",origSrc,"\nMoving to - ",getenv('HOME')+dest)
    try:
        shutil.move(origSrc,getenv('HOME')+dest)
    except shutil.Error as e:
        print(e)
        pass

def patternMatching(fileName):
    for pattern in d["Patterns"]:
        for pathis,extensionList in zip([d["Patterns"][pattern]["path"]],[d["Patterns"][pattern]["extensions"]]):
            for ext in extensionList:
                if fnmatch.fnmatch(fileName,ext):
                    moveFiles(fileName,pathis)


def interactiveMode():
    for folder in folderList:
        letter = input("Where Shall I move "+folder+". m-music,d-Documents,v-Videos,p-Pictures\n")
        if (letter=="M" or letter=="m"):
            moveFiles(folder,d["Patterns"]["Music"]["path"])
        elif (letter=="D" or letter=="d"):
            moveFiles(folder,d["Patterns"]["Documents"]["path"])
        elif (letter=="V" or letter=="v"):
            moveFiles(folder,d["Patterns"]["Videos"]["path"])
        elif (letter=="P" or letter=="p"):
            moveFiles(folder,d["Patterns"]["Pictures"]["path"])
        else:
            print("Skipping the folder")

if __name__=="__main__":
    for file in fileList:
        patternMatching(file)
    interactiveMode()

以上是关于python Sortr(基于模式的下载文件夹分拣机)的主要内容,如果未能解决你的问题,请参考以下文章

打造图像识别分拣系统斩获出乎意料之效益 | 经典重温

焦点报道打造图像识别分拣系统斩获出乎意料之效益

手持终端以物联网的模式

knn分类10折工具和分拣

基于python的文件处理

物流分拣流水线的优点及功能