Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...