[Python] moving files in a new folder in Python using shutil

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to do a simple script that iterates all the content in a folder and then if an element is a image it gets moved to a subfolder (inside the original folder) called images, while if it is video it gets moved in a subfolder (inside the original folder) called videos. This is my code:

    import os
    import shutil

    path = input('file path to sort: ')
    list = os.listdir(path)

    fin_img_path = os.mkdir(path+'\\images')
    fin_vid_path = os.mkdir(path+'\\videos')

    for i in list:
    print(i)
    if i.endswith('.jpg'):
    new_path = shutil.move(f"{path}/{i}", fin_img_path)
    elif i.endswith('.mp4') or i.endswith('.mkv'):
    new_path = shutil.move(f"{path}/{i}", fin_img_path)
    print(new_path)

    print('images and videos divided')


    However I get this error message:

    file path to sort: C:\Users\utente\Pictures\Saved Pictures\Images_Videos
    1.jpg
    Traceback (most recent call last):
    File "c:\vscode\PYTHON\image-videos.py", line 13, in <module>
    new_path = shutil.move(f"{path}/{i}", fin_img_path)
    File "C:\Python310\lib\shutil.py", line 791, in move
    if os.path.isdir(dst):
    File "C:\Python310\lib\genericpath.py", line 42, in isdir
    st = os.stat(s)
    TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType


    Anyone has any idea how to fix this?

    Continue reading...

Compartilhe esta Página