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?
#1
Hello!

Like many, I am currently learning python and working on my first project. It is a script that changes all the files within a directory based on user input. Currently built for TV Show file changes.

so far its going well, I'm just stuck on one thing. I feel like the answer is on the tip of my tongue, like I've heard it in previous lessons I took, I just can't quite remember.

At a certain point in the script, to handle some episodes that may be "2 in 1s", it asks the user to specify if there are Episodes that are joined together (ex one .mp4 file titled "Episode 1, Episode 2"). if the user answers "y", it then asks how many instances this occurs (ex. 2). the user then specifys the # of times, and then the loop asks the same question depending on how many times the user specified (First Episode?, Second Episode?).

This is essentially the process:

(python) Are there multi episodes?
(user) Y

(python) How many?
(user) 2

(python) First Episode?
(user) 1
(python) Second Episode?
(user) 2

(python) First Episode?
(user) 5
(python) Second Episode?
(user) 6

The script should then rename "Episode 1, Episode 2" to E01E02 and "Episode 5, Episode 6" to E05E06. However, the end result is only E05E06 is changed, what i want to be changed to "E01E02" stays at its original title, "Episode 1, Episode 2".

I believe it does change, but the next loop overwrites it, thus only the latest user response changes.

Is there a way to get the loop to keep user changes each time it runs through?

I'm thinking it is a problem with my eptokea and eptokeb strings, just not sure how to go about fixing it

Any help is greatly appreciated!

SAMPLE CODE

        
                epstochange = int(input("How many multi eps? "))
                for i in range(epstochange):
                    i += 1
                    print("Which episodes are joined?")
                    eptokea = input("First Episode: ")
                    eptokeb = input("Second Episode: ")
                    # Starts renaming loop if user specifies tv shows are multi episode
                    path = os.getcwd()
                    os.chdir(filenamedir)
                    filenames = os.listdir(filenamedir)
                    COUNT = 1

                    def increment():
                        global COUNT
                        COUNT = COUNT + 1

                    for f in filenames:
                        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)
                        else:
                            f_name = showname + " - " + "S" + season.zfill(2) + "E" + str(COUNT).zfill(2)
                            increment()

                            new_name = '{} {}'.format(f_name, f_ext)
                            os.rename(f, new_name)
Reply
#2
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.
hbkpancakes likes this post
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stop/continue While loop block Moris526 42 1,014 10 hours ago
Last Post: Moris526
  Color Formatting for Bar Graphs in a for loop adamszymanski 0 69 Jan-17-2021, 10:05 PM
Last Post: adamszymanski
  How do I add another loop to my nested loop greenpine 11 513 Jan-12-2021, 04:41 PM
Last Post: greenpine
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 163 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Convert string to JSON using a for loop PG_Breizh 3 118 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  how to create pythonic codes including for loop and if statement? aupres 1 163 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 145 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  Matrix indexing and initialization in " for in" loop QuintenR 2 146 Dec-23-2020, 05:59 PM
Last Post: QuintenR
  websockets server loop? korenron 1 142 Dec-23-2020, 03:59 PM
Last Post: korenron
  Noob Alert! Wrong result using loop and if statemnent GJG 7 307 Dec-19-2020, 05:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

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