使用Shutil将文件移动文件夹中的新文件夹中的文件
我正在尝试执行一个简单的脚本,该脚本会迭代文件夹中的所有内容,然后如果元素是图像,则将其移至子文件夹(在原始文件夹中),称为images
,而如果视频是在一个子文件夹(原始文件夹中)中移动的,称为videos
。 这是我的代码:
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')
但是我收到此错误消息:
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
有人知道如何解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
os.mkdir()
是一个程序调用,返回无
,因此您的错误。您需要将文件路径的名称取代到shutil.move()
,例如os.mkdir()
is a procedural call and returnsNone
, hence your error. You need to feed the name of the file path instead toshutil.move()
, e.g.