##### Select correct item from list for subprocess command
 Select correct item from list for subprocess command pythonnewbie138 Silly Frenchman Posts: 37 Threads: 11 Joined: Jul 2020 Reputation: Jul-24-2020, 06:15 PM (This post was last modified: Jul-24-2020, 06:15 PM by pythonnewbie138.) I'm having trouble getting my user input into a subprocess command. Everything seems to work fine until it takes the screenshot and it ends up choosing the last item in the list instead of the entered number. I have 4 files in the folder that fit the endswith requirement and they all print correctly. Any help with this would be much appreciated. import os import subprocess user_choice = input("Enter a directory: ") location = os.scandir(user_choice) list1 = [] for file in location: if file.name.endswith(('.mp4', '.mkv')): list1.append(file.name) for count, ele in enumerate(list1, 1): print(str(count) + ".", ele) f1 = input("Select a file: ") f2 = list1[int(f1)] subprocess.run(['ffmpeg', '-i', f2, '-ss', '00:00:02', '-vframes', '1', f'{file.name.split(".")[0]}.png']) Reply Posts: 2,018 Threads: 35 Joined: Sep 2016 Reputation: Jul-24-2020, 06:36 PM (This post was last modified: Jul-24-2020, 06:36 PM by Yoriz.) In the line subprocess.run(['ffmpeg', '-i', f2, '-ss', '00:00:02', '-vframes', '1', f'{file.name.split(".")[0]}.png'])file will be the last file asigned in the loop by location as it has not been defined as anything since then. In the line f2 = list1[int(f1)]f2 has been assigned as the chosen item from the list and then nothing is done with it. Reply pythonnewbie138 Silly Frenchman Posts: 37 Threads: 11 Joined: Jul 2020 Reputation: Jul-24-2020, 07:59 PM I think I'm lacking understanding of what I'm doing with file early in the script. I'm actually unsure of how file is created in the first place. There's no strict definition at the beginning. Googling hasn't yielded anything useful yet. I tried to manually assign f2 as the value for file with file = str(f2) before the subprocess command but that yields the following error: Error:AttributeError: 'str' object has no attribute 'name'I didn't think it would work but I'm at a loss at the moment. Reply Posts: 2,018 Threads: 35 Joined: Sep 2016 Reputation: Jul-24-2020, 08:27 PM (This post was last modified: Jul-24-2020, 08:27 PM by Yoriz.) (Jul-24-2020, 07:59 PM)pythonnewbie138 Wrote: I think I'm lacking understanding of what I'm doing with file early in the script. I'm actually unsure of how file is created in the first place. There's no strict definition at the beginning. Googling hasn't yielded anything useful yet. for file in location:file is assigned the value of each item in location. if location was a list [0, 1, 2, 3] as it looped through each time file = 0 # first loop file = 1 # second loop file = 2 # third loop file = 3 # forth loopfile would now by pointing to 3 after the loop (Jul-24-2020, 07:59 PM)pythonnewbie138 Wrote: I tried to manually assign f2 as the value for file with file = str(f2) before the subprocess command but that yields the following error: Error:AttributeError: 'str' object has no attribute 'name'I didn't think it would work but I'm at a loss at the moment. In the loop you have list1.append(file.name)so list1 now contain strings of the file names when the selection is made f2 = list1[int(f1)]f2 now contains the chosen filename string f2.split(".")[0]would give the filename string less its extension. Reply pythonnewbie138 Silly Frenchman Posts: 37 Threads: 11 Joined: Jul 2020 Reputation: Jul-24-2020, 08:42 PM Thanks for breaking it down. So helpful! This works now! subprocess.run(['ffmpeg', '-i', f2, '-ss', '00:00:02', '-vframes', '1', f'{f2.split(".")[0]}.png'])There's 1 part left that I'm unsure how to approach. Currently the selection is based on the Index (0-3 in this situation). How can I align the enumerated list with the index? The script prints "1", "2", etc in front of the printed filenames based on the count but the input is based on the index location. How can I make the input options (1-4) match the index options (0-3) so an input of 1 points to index 0? I initially thought the enumeration starting at 1 would do this but apparently that's not the case. Reply Posts: 2,018 Threads: 35 Joined: Sep 2016 Reputation: Jul-24-2020, 08:54 PM You could reduce the input number by 1 f1 = input("Select a file: ") f2 = list1[int(f1)-1] Reply pythonnewbie138 Silly Frenchman Posts: 37 Threads: 11 Joined: Jul 2020 Reputation: Jul-24-2020, 09:09 PM (Jul-24-2020, 08:54 PM)Yoriz Wrote: You could reduce the input number by 1 f1 = input("Select a file: ") f2 = list1[int(f1)-1] Wow that was simple. I learned a lot from this thread. Thank you so much! Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Finding string in list item jesse68 8 343 Jun-30-2022, 08:27 AM Last Post: Gribouillis select Eof extension files based on text list of filenames with if condition RolanRoll 1 559 Apr-04-2022, 09:29 PM Last Post: Larz60+ how to easily create a list of already existing item CompleteNewb 15 1,388 Jan-06-2022, 12:48 AM Last Post: CompleteNewb Remove an item from a list contained in another item in python CompleteNewb 19 2,552 Nov-11-2021, 06:43 AM Last Post: Gribouillis use subprocess on linux\pi wwith a "grep " command korenron 2 1,634 Oct-19-2021, 10:52 AM Last Post: DeaD_EyE deleting select items from a list Skaperen 13 2,250 Oct-11-2021, 01:02 AM Last Post: Skaperen count item in list korenron 8 1,870 Aug-18-2021, 06:40 AM Last Post: naughtyCat Time.sleep: stop appending item to the list if time is early quest 0 1,228 Apr-13-2021, 11:44 AM Last Post: quest How to run a pytest test for each item in a list arielma 0 1,379 Jan-06-2021, 10:40 PM Last Post: arielma How do I add a number to every item in a list? john316 2 1,096 Oct-28-2020, 05:29 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