pythonstringos.pathfile-move

Why do I get " stat: path should be string, bytes, os.PathLike or integer, not tuple" type error?


I am trying to group files into folders based on the prefix of the filename. Error: os.stat(path)

TypeError: stat: path should be string, bytes, os.PathLike or integer, not tuple

I am getting the error on the line that corresponds to 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)
        

Any insights as to where I did it wrong? Thank you.


Solution

  • Change the line to dir_path = file[0][:-8].
    According to the doc, os.walk() yields a tuple: (dirpath, dirnames, filenames), therefore file in your code is a tuple containing dirpath, dirnames, and filenames.