使用Shutil将文件移动文件夹中的新文件夹中的文件

发布于 2025-01-18 07:51:13 字数 1177 浏览 3 评论 0原文

我正在尝试执行一个简单的脚本,该脚本会迭代文件夹中的所有内容,然后如果元素是图像,则将其移至子文件夹(在原始文件夹中),称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2025-01-25 07:51:13

os.mkdir()是一个程序调用,返回,因此您的错误。您需要将文件路径的名称取代到shutil.move(),例如

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文