Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - I made a py script that puts files with similar names in folders but it nested folders one inside another

I have a folder with all of my anime inside it, but they are formatted in numbers, example: "3321_13.mp4", "3321_14.mp4". It was pretty messy and the folder started to get even messier with rar files,pngs, .exes, etc. So I had too much time in my hands today and decided to make a script to do all the sorting for me, I searched for the os lib documentation and shutil too a few hours later. I made the script, it worked in a test folder everything looked fine but decided to take a redundancy out. When I ran the script in the actual folder that needed sorting, every .mp4 went inside it's own group folder as expected. Nice, I said, until I opened a folder and found another folder with the same group id name, and then another, and another, and so forth. Eventually you can reach the .mp4 files grouped but I can't think of a reason with it made so many subfolders.

Here's the script:

import os
import shutil
videoid = ''
videogid = ''
count = 0
folders = []


with os.scandir(".") as it: #opens and scans the scripts current directory
    for entry in it: #iterates every entry in the directory list
        if not entry.name.startswith('.') and entry.is_file() and entry.name.endswith('.mp4'): 
 #tests each file to see if it-s whether a mp4 file or not
            for x in entry.name: #iterates every character in the entry's name string
                if count < 4: #reads the 4 first characters
                    videoid = videoid + x
                    count = count + 1
                else:
                    break
            try:
                os.mkdir(videoid)
            except FileExistsError:
                pass
            finally:
                shutil.move(entry.path, videoid)
                print(videoid)
                videoid= ''
                count = 0

the videogid variable is the redundancy I mentioned earlier: originally it was:

              else:
                    break
            videogid= videoid
            try:
                os.mkdir(videogid)
            except FileExistsError:
                pass
            finally:
                shutil.move(entry.path, videogid) 

I figured that since it's going to read only the first 4 digits and make a directory from it for every file videogid was going to do nothing.

Every .mp4 is in it's correspondent group at the very last folder. What I don't understand is the loads of unnecessary subfolders. screencap for a better image of the fallout

I learnt to use os and shutils as I did this I consider it a semi-success. Any feedback is much appreciated.

question from:https://stackoverflow.com/questions/65645341/i-made-a-py-script-that-puts-files-with-similar-names-in-folders-but-it-nested-f

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...