如何解决为什么我得到“stat: path should be string, bytes, os.PathLike or integer, not tuple”类型错误?
我正在尝试根据文件名的前缀将文件分组到文件夹中。错误:os.stat(path)
TypeError: stat: path 应该是字符串、字节、os.PathLike 或整数,而不是元组
我在对应于 dir_path = file[:-8]
import os
import pickle
from os.path import join,exists
import shutil
RootDir = r'D:\Folder'
count = 0
for file in os.walk((os.path.normpath(RootDir)),topdown=False):
dir_path = file[:-8]
if not os.path.exists(dir_path):
os.makedirs(dir_path)
if os.path.exists(dir_path):
shutil.move(file)
关于我哪里做错了的任何见解?谢谢。
解决方法
将行更改为 dir_path = file[0][:-8]
。
根据 doc,os.walk()
产生一个元组:(dirpath,dirnames,filenames)
,因此代码中的 file
是一个包含目录路径、目录名和文件名的元组。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。