Python Forum
File sorting by user-chosen category
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File sorting by user-chosen category
#1
Hi, y'all!
What I want to do is to sort epub files into category-based folders, I've got multiple categories and figured out a separator I'll put into every filename (cumbersome, yes, but I'll deal with that later). I've managed to figure out how to do it the crude way (see attached code). I know it could be done waaay better, because it's sloppy (and not very pythony). Furthermore, I'd love the script to auto-search the „category” (a tag) within the filename and create folder by itself.
Any help will be appreciated.
Thanks in advance!
        
        homedir = os.path.expanduser("~")
        path = os.path.join(homedir, 'OneDrive/PC/downloads')
        dst = os.path.join(homedir, 'OneDrive/Philosophy')
        files = os.listdir(path)
        increment = 0
        for file in files:
            name, ext = os.path.splitext(file)
            if fnmatch.fnmatchcase(file, '*; Philosophy.*'):
                if os.path.exists(dst) is True:
                    while os.path.exists(dst+'/'+f'{name}{ext}'):
                        if isinstance(increment, str):
                            increment = -1
                        increment += 1
                        try:
                            os.rename(dst+'/'+file, dst+'/'+f'{name}_{increment}{ext}')
                            return False
                        except Exception:
                            return True
                else:
                    os.makedirs(dst)
                shutil.move(os.path.join(path, file), dst)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ploting single column with multiple category drunkenneo 1 1,979 May-26-2021, 04:51 PM
Last Post: jefsummers
  Problems Sorting Data in an External File (.txt) Superlegend21 1 4,320 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  User Input and CSV File Davy_Jones_XIV 2 4,803 Mar-24-2020, 08:43 AM
Last Post: Davy_Jones_XIV
  Sorting columns from text file help nickglobal101 1 3,588 Nov-15-2019, 07:05 PM
Last Post: newbieAuggie2019
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,044 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  Sorting csv or txt file on date and time AshBax 4 8,315 Mar-27-2019, 02:02 PM
Last Post: AshBax
  Read from a file, get user input in a while loop lynden 8 4,126 Nov-05-2018, 12:31 PM
Last Post: lynden
  Accept Multiple Lines of Input into a User Created File Bragger 0 2,606 Oct-29-2017, 04:15 PM
Last Post: Bragger
  Shutil problem in file sorting script xavier992 3 3,991 Jun-22-2017, 08:24 PM
Last Post: Larz60+
  Sorting File iainstott 2 5,301 Dec-28-2016, 05:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020