简体   繁体   中英

How to check for a particular file or directory in a folder and move it bases on certain criteria, in python

I want to create a python junk manager. For this let's say we have a png a doc file and two directory named cars and bikes in a folder. I have a python script which can sort these file in Image , Document and PDF folders respectively. Now in addition to this, i also want to put those two directories names cars and bikes in a folder named MISC . The way i would like to do this is moving all the folders whose names do not match with the folders created by the script.

Here is the script:

from pathlib import Path 

DIRECTORIES = { 
    "HTML": [".html5", ".html", ".htm", ".xhtml"], 
    "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", 
            ".heif", ".psd"], 
    "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", 
            ".qt", ".mpg", ".mpeg", ".3gp"], 
    "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", 
                ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", 
                ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", 
                "pptx"], "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", 
                ".dmg", ".rar", ".xar", ".zip"], 
    "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", 
            ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], 
    "PLAINTEXT": [".txt", ".in", ".out"], 
    "PDF": [".pdf"], 
    "PYTHON": [".py"], 
    "XML": [".xml"], 
    "EXE": [".exe"], 
    "SHELL": [".sh"] 

} 

FILE_FORMATS = {file_format: directory 
                for directory, file_formats in DIRECTORIES.items() 
                for file_format in file_formats} 

def organize_junk(): 
    for entry in os.scandir(): 
        if entry.is_dir(): 
            continue
        file_path = Path(entry) 
        file_format = file_path.suffix.lower() 
        if file_format in FILE_FORMATS: 
            directory_path = Path(FILE_FORMATS[file_format]) 
            directory_path.mkdir(exist_ok=True) 
            file_path.rename(directory_path.joinpath(file_path)) 

        for dir in os.scandir(): 
            try: 
                os.rmdir(dir) 
            except: 
                pass

if __name__ == "__main__": 
    organize_junk() 

And please don't downgrade the question it means something to me! Any assistance will be appreciated. Thanks!

You seem to already have a block that checks whether you're looking at a directory, it just doesn't do anything yet:

if entry.is_dir(): 
    continue

It seems like you just want to create a new directory called 'MISC' whenever you encounter a directory that's not one of the keys in your 'DIRECTORIES' dict. Something like:

if entry.is_dir():
    if <the directory's name is not in DIRECTORIES>:
        # Create directory 'MISC', then move entry into 'MISC'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM