Python Forum
Loop back through loop based on user input, keeping previous changes loop made?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop back through loop based on user input, keeping previous changes loop made?
#3
(Nov-19-2020, 04:28 AM)bowlofred Wrote: This is really complex. It looks like every time through the outer loop you rename every file. That doesn't make sense to me.

for i in range(epstochange):  # 2 in your example, with 2 sets to rename
    ...
    for f in filenames:       # looping over all the files...
        f_name, f_ext = os.path.splitext(f)
        if str(COUNT) == eptokea.format(i):
            def incrementx():
                global COUNT
                COUNT = COUNT + 2
            f_name = showname + " - " + "S" + season.zfill(2) + "E" + eptokea.format(i).zfill(2) + "E" + eptokeb.format(i).zfill(2)
            incrementx()
 
            new_name = '{} {}'.format(f_name, f_ext)
            os.rename(f, new_name)
Note that nothing in this loop checks what file "f" is. You just split the filename and rename it. So all the files get renamed every time through the loop.

I would expect you would want to check which file was found by the for f in filenames and only rename if it matched some pattern or similar.

so what i failed to mention is, i still want the script to rename every file, but rename the files specified in the "double" format.

ex) there are 10 tv show files in a folder, 2 are multijoined episodes, Episodes 4 & 5, and Episodes 8 & 9.

I want the user to be able to specify there are 2 multijoined episodes, say they are 4 and 5, and 8 and 9, and then the program renames all the files in the folder as so:

S01E01
S01E02
S01E03
S01E04E05 <-
S01E06
S01E07
S01E08E09 <-
S01E10

I already got it down to where it can rename all the episodes like that when there arent joined episodes, and I even got it down to the point where it can name 1 multijoined episode, its just when there are more than one, it only renames the last specified multijoined episode.

I've since realized I may need to incorporate lists in the loop, but still can't get it to work. I can get the lists to capture the specified episodes (so in the above example, List A would have "'4','8'" in it, and List B would have "'5','9'", and i can get the loop to reference the instance in the list and rename 1 episode (so it would rename one episode to S01E04E05), but cant get it to loop back through and rename the next instance.

I think the main problem now is to figure out how to successfully increment and reference indexes in a ist
Reply


Messages In This Thread
RE: Loop back through loop based on user input, keeping previous changes loop made? - by hbkpancakes - Nov-21-2020, 02:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loop not executing Abendrot47 2 190 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 562 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 788 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 480 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 352 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 387 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 432 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 607 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,172 Dec-07-2023, 12:28 PM
Last Post: RockBlok
  Loop over an an array of array Chendipeter 1 574 Nov-28-2023, 06:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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