问题描述
我正在尝试根据文件名的前缀将文件分组到文件夹中。错误: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
是一个包含目录路径、目录名和文件名的元组。