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
#11
(Aug-13-2020, 12:01 PM)GOTO10 Wrote: When you pass the path variable to os.chdir(), you need it to be a valid directory like 'C:\TMP'. When you pass the path variable to glob.glob() (lines 18 and 36), you need it to include a wildcard like 'C:\TMP\*'. My guess is you are passing path without a wildcard in lines 18 and 36, so your glob.glob() loop is not actually finding anything that satisfies the if condition (nothing that ends with .jpg or .png).

import glob

print('Printing items using C:\TMP as path')
for item in glob.glob('C:\TMP'):
    print(item)
    
print('Printing items using C:\TMP\* as path')
for item in glob.glob('C:\TMP\*'):
    print(item)
Output:
Printing items using C:\TMP as path C:\TMP Printing items using C:\TMP\* as path C:\TMP\Testing.jpg C:\TMP\Placeholder.jpg

Thank you - I have managed to make some changes so that the code for resizing works again. So that part is all sorted. I just now need to make sure that all images save into the directory created (which would be a user path to a folder named thumbnails - already created in line seven (please see below)

from PIL import Image
import os

imgs = []
path = input("Where are your images? > ")
x = os.chdir(path)
new_path = os.makedirs('Thumbnails')

valid_images = [".jpg",".gif",".png",".tga", ".bmp", ".svg", ".tif"]
for f in os.listdir(path):
    ext = os.path.splitext(f)[1]
    if ext.lower() not in valid_images:
        print ("This folder contains invalid file types - please check again. ")
        continue
    imgs.append(Image.open(os.path.join(path,f)))
    print(f)
    viewimgs = Image.open(os.path.join(path,f))
    #viewimgs.show()
    fn, fext = os.path.splitext(f)
    
    new_height = int(input("Height of thumbnail (in pixels)? "))
    new_width = int(input("Width of thumbnail (in pixels)? "))
    new_size = viewimgs.resize((new_width, new_height))
    new_size.show()
    
    new_size.save(new_path + fext)
How do I adjust line 26 to make sure that it saves in the user path subdirectory named "thumbnails" and not in the original folder?

Many thanks,
paulmerton4pope
Reply


Messages In This Thread
RE: Changing Directory based on user input - by paulmerton4pope - Aug-13-2020, 02:24 PM

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,347 Nov-23-2022, 07:53 PM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,307 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,407 Nov-07-2020, 07:01 PM
Last Post: buran
  sys.stdin to do a word count based on user entry Kaltex 3 3,713 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  how to add the user input from file into list wilson20 8 4,355 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,841 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,191 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,066 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,535 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,598 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