Python Forum
Changing Directory based on user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Directory based on user input
#14
I think you just need to rethink your logic in the for loop at line 10 and your error condition/message in lines 12-14. You are currently looping through all of the contents of the file path and printing the error each time an item doesn't have one of your chosen extensions. You are guaranteed to encounter the error at least once per execution, because the Thumbnails folder is going to be one of the items in the path, and that will trigger the message since it doesn't have an extension. It would probably make more sense to print the error only if there are no valid image files to work with, but otherwise just ignore any non-image files as long as there is at least one valid image in the path.

There are lots of ways to approach this, but one thing that might make it easier is to first create a list of items in the path, then work with the list:

file_list = [f for f in os.listdir(path)]
# put a check here to confirm that at least one valid image is present in file_list, otherwise print error and start over
# after confirming there is at least one valid image, remove non-images from file_list
# loop through file_list, which now contains only valid images in the path, to perform image manipulations
Reply


Messages In This Thread
RE: Changing Directory based on user input - by GOTO10 - Aug-14-2020, 11:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a menu and execute a function based on user selection in python? Thedarkphoenix 1 1,326 Nov-23-2022, 07:53 PM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,288 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,384 Nov-07-2020, 07:01 PM
Last Post: buran
  sys.stdin to do a word count based on user entry Kaltex 3 3,684 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  how to add the user input from file into list wilson20 8 4,336 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,817 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,163 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,043 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,516 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,586 Mar-11-2019, 07:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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