Skip to content

Instantly share code, notes, and snippets.

@rors
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?
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 ...
@rors
Copy link
Author

rors commented Feb 12, 2021

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 using random.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 a print() statement after line 6 that checks if 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 uses directory_name to open that directory and returns a list of all the files in that directory, which we save here in a variable called files.

Then, line 10 uses the random.choice() function to select one random filename from the files list, which then gets saved in the variable named file1. Line 11 then uses the command from last week to open() the file whose name is saved in file1.

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 called project, and I had a subdirectory called images containing a file called spaghetti.jpg, I would specify that in Python as images/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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment