/unit1-exercise4.py Secret
Created
February 12, 2021 16:20
Question: I understand how to create a list of files from a directory in the Python shell, but I'm confused about how to use the user input (command line arguments) as a directory in my Python file. How do I use the argument to specify a random photo from within the directory?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from os import listdir | |
import random | |
from PIL import Image | |
directory_name = sys.argv[1] | |
files = listdir(directory_name) | |
file1 = random.choice(files) | |
img1 = Image.open( directory_name + "/" + file1 ) | |
file2 = random.choice(files) | |
img12 = Image.open( directory_name + "/" + file2 ) | |
# etc ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a question I've received about the current homework, and I thought others might benefit from seeing my response.
The explanation here assumes that you understand the various pieces that we're working with (command line arguments, directory listing, opening an
Image
file, and usingrandom.choice()
). If any of these are still confusing to you, refer back to the class notes from Weds, Feb 10, and if you're still confused after that, ask me.This code snippet illustrates how to stitch those things together.
On line 6 you can see the code from last week that accesses an argument specified by the user on the command line. (This is imported on line 1.) After this line,
directory_name
will be a variable that holds the text that the user specified, which we will assume is a directory name. For greater usability and user-friendliness, you could add aprint()
statement after line 6 that checksif len(sys.argv) < 2
and if so, informs the user that one command line argument is required, and it should be the name of a directory.The
listdir()
command on line 8 (imported on line 2) then usesdirectory_name
to open that directory and returns a list of all the files in that directory, which we save here in a variable calledfiles
.Then, line 10 uses the
random.choice()
function to select one random filename from thefiles
list, which then gets saved in the variable namedfile1
. Line 11 then uses the command from last week toopen()
the file whose name is saved infile1
.Probably the most confusing part of this is the fragment on line 11 that reads:
directory_name + "/" + file1
. This is doing something that I did not clearly explain in class. Since you are trying to open a file that is not in the "current" directory (the directory in which you ran this program) but instead are opening a file in a subdirectory, you have to specify that subdirectory by name when specifying the file that you want to open. For example, if I was in a directory calledproject
, and I had a subdirectory calledimages
containing a file calledspaghetti.jpg
, I would specify that in Python asimages/spaghetti.jpg
. What we're doing here is specifying the file in that way, but instead of using text directly, we're using variables that contain those text values. This is "a command line thing", but it is also a thing in nearly all contexts of computation. Programming for web servers, web browsers, apps, or desktop applications will all use similar techniques for referencing files. Usually we use Finder or Explorer to access files in a file system – this type of specification is how we do that in computer programs.Lines 13-14 simply repeat lines 10-11: selecting another filename randomly from the list and opening it as an
Image
. You can then repeat this pattern to open additional images.A couple warnings: this code assumes that the user's command line argument is a valid directory name, that this directory contains files, and that those files are all only images. If any of these assumptions are wrong, this will throw an error and crash. We can talk about better error handling in the future. But for now, just make sure that all those conditions are true when you run this.
Hope that helps.