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

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

python - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...