Reputation: 457
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?
Upvotes: 0
Views: 927
Reputation: 41228
os.mkdir()
is a procedural call and returns None
, hence your error. You need to feed the name of the file path instead to shutil.move()
, e.g.
fin_img_path = path+'\\images'
fin_vid_path = path+'\\videos'
os.mkdir(fin_img_path)
os.mkdir(fin_vid_path)
Upvotes: 2