0

I'll be referring to python in this question. Ok, let's say i have a script that renames 1000 files.How do i multithread it? I thought about dividing the files into chunks but it makes the code too complex, and to run 1000 thread, one for each file, isn't going to work.

Here is the code.

import os
import tkinter.messagebox
import tkinter.ttk
import tkinter.filedialog
from   tkinter import Tk, StringVar, Text, END

print("""
    A little reminder!
    Any file that gets renamed can't be undo,so don't screw around with this.
    NOTE : Careful with the extension,you might lose some files this way.
    """)

class Rename:

    def __init__(self,path): self.p = path

    def rename(self):
        try:

            files = os.listdir(self.p)
            i = 0
            os.chdir(self.p)
            while i <= len(files):
                ext = files[i].split(".")[-1]
                #You can change the renaming variable,for example you may add "file{}.{}".format(i,ext)..
                #also,don't play with the ext if you don't know what you're doing.. 
                os.rename(files[i],"{}.{}".format(i,ext))
                i += 1
                yield "Now renaming {}".format(files[i])

        except OSError as e: print("Error -> {}".format(e))
        except IndexError: pass

class GUI:

    def __init__(self):
        self.root = Tk()
        self.root.wm_title("FileRenamer")
        self.root.config(background = "black")

        # Label
        self.label = tkinter.ttk.Label(self.root, text = "Path: ")
        self.label.config(foreground = "white", background = "black", font = ("Arial", 16))
        self.label.grid(row = 0, column = 0)

        # File dialog
        self.path = StringVar()
        self.filepath = tkinter.ttk.Button(self.root, text = "Choose", command = self.askPath).grid(row = 0, column = 1)

        # TextWidget
        self.text = Text(self.root, height = 5, width = 50)
        self.text.config(foreground = "white", background = "black", font = ("Arial", 12))
        self.text.grid(row = 3, column = 0, columnspan = 2)

        # Button
        self.button = tkinter.ttk.Button(self.root, text = "Rename", command = self.rename)
        self.button.grid(row = 2, column = 1, columnspan = 2)

        self.root.mainloop()

    def askPath(self):
        self.path = tkinter.filedialog.askdirectory()

    def rename(self):
        path = os.path.abspath(self.path)
        rem  = Rename(path)
        for im in rem.rename():
            self.text.insert(END, "{}\n".format(im))

# Make sure that you add another "\" to the path you enter, otherwise you'll get an error.
r = GUI()
  • related (not dupe) https://stackoverflow.com/questions/481970/how-many-threads-is-too-many and https://stackoverflow.com/questions/19369724/the-right-way-to-limit-maximum-number-of-threads-running-at-once – IronManMark20 Jun 25 '15 at 15:24
  • Create an appropriate sized pool of worker threads, and use messaging to coordinate them. When a worker is ready (done it's last job), give it the next one on the queue until you have no jobs left to do. Tip: use the Process class from multiprocessing for your workers (don't use Thread). – Jim Wood Jun 25 '15 at 16:04

0 Answers0