0

I'm trying to get this script to take the contents of the tuple and cycle through using a for loop (I'm not sure where to put it in my code) and put the contents of the tuple in a command. For this example I've used find as the command. Depending on which option the executor uses sp1 or sp2 will determine how much of the tuple will be used.

import sys, subprocess, os, string

cmd = '/bin/find '

tuple = ('apple', 'banana', 'cat', 'dog')

sp1 = tuple[0:1]
sp2 = tuple[2:3]

def find():
    find_cmd = subprocess.Popen(cmd + " * -name {}".format(type)),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE, shell=True)
    output, err = find_cmd.communicate()
    find = output
    find_temp = []
    find_temp = string.split(find)
    if find_temp[0] == ' ':
        print("Found nothing")
    else:
        print("Found {}".format(find_temp))

type_input = input("Are you looking for fruit or animals? ")
if type_input  == "fruit":
    type = sp1
elif type_input == "animals":
    type = sp2
    print("syntax error")
    exit()

find()
2
  • 2
    an aside, be careful when naming variables that shadow built-in keywords (e.g. tuple) because they will override the name of the built-in in that scope. stackoverflow.com/a/2418007/1248974 Commented Dec 7, 2016 at 18:42
  • What exactly is the problem you're having? Commented Dec 7, 2016 at 18:49

1 Answer 1

0

You're close, but you don't do what you're trying to do, that's just silly. You could do better.

Rather than doing this weird tuple slicing thing, just give them a real name:

import sys, subprocess, os, string

# No need for the trailing space here
FIND = '/bin/find'

fruit = ('apple', 'banana')
animals = ('cat', 'dog')

Alternatively, you could use a dictionary:

find_params = {
    'fruit': ['apple', 'banana'],
    'animals': ['cat', 'dog'],
}

In your comment you mentioned:

my tuple is a bit larger and both variables use some of the same values... This would keep me from typing many of the same values into two separate lists.

You can still take a nice approach:

cheeses = ('Wensleydale', 'Edam', 'Cheddar', 'Gouda')
spam = ('Spam with eggs', 'Spam on eggs', 'Spam')
confectionaries = ('crunchy frog', 'spring surprise', 'cockroach cluster',
                   'anthrax ripple', 'cherry fondue')
food = cheeses + spam + confectionaries

Even if you just need a subset, you can still do something like:

food = cheeses + spam[:2] + confectionaries[-1:]

You should take parameter(s) for your find command instead. Also, no need to concatenate and then use a format string. Just use a format string for all the things:

def find(what, cmd=FIND):
    find_cmd = subprocess.Popen('{cmd} * -name {what}'.format(cmd=cmd, what=what),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE, shell=True)
    output, err = find_cmd.communicate()
    find = output
    find_temp = []
    find_temp = string.split(find)
    if find_temp[0] == ' ':
        print("Found nothing")
    else:
        print("Found {}".format(find_temp))

Now you can either use the variables, or what they asked for:

type_input = input("Are you looking for fruit or animals? ")
try:
    find(find_params[type_input])
except KeyError:
    sys.exit('Unknown parameter {!r}'.format(type_input))

# Or use the variables
if type_input  == "fruit":
    find(fruit)
elif type_input == "animals":
    find(animals)
else:
    sys.exit('Unknown parameter {!r}'.format(type_input))
Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant! Thank you for sharing. Now, it's my fault for not clearly explaining my issue, but in my real world issue my tuple is a bit larger and both variables use some of the same values. For example sp1 = tuple[0:10] and sp2 = tuple[0:20]. This would keep me from typing many of the same values into two separate lists.
I'd still give them more appropriate names than sp1 and sp2. I'll update my question with some examples.
Thank you for updating your answer. This really helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.