Python Forum

Full Version: Calling a Returned Value to Another Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Gigs,

I'm trying to call a value obtained from a function to another function. In this case, i want to call var_name into get_dirs_from_path() function.
Below is what i did, but it's not working.
NB:  get_dirs_from_path()  must not have a parameter.

#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
    var_name = os.environ[variable_name]
    if not var_name:
        return ""
    else:
        return var_name

def get_dirs_from_path():
    path_var = var_name
    directories = path_var.split(':')
    for entry in directories:
        print (entry)
Please advise.
Smile
You can call one function from inside another.

def get_dirs_from_path():
    path_var = get_environment_variable_value(some_env_var_name)
    if path_var:
        directories = path_var.split(':')
        for entry in directories:
            print (entry)
What should be the "some_env_var_name" in this case ?

When i test the code below, 

#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
    var_name = os.environ[variable_name]
    if not var_name:
        return ""
    else:
        return var_name

def get_dirs_from_path():
    path_var = get_environment_variable_value(variable_name)
    directories = path_var.split(':')
    for entry in directories:
        print (entry)


get_environment_variable_value('PATH')
get_dirs_from_path()
it prints the following output:
Output:
psimo@itserver6:~/it117/hw/hw6$ ./hw66.py Traceback (most recent call last):   File "./hw66.py", line 22, in <module>     get_dirs_from_path()   File "./hw66.py", line 15, in get_dirs_from_path     path_var = get_environment_variable_value(variable_name) NameError: name 'variable_name' is not defined psimo@itserver6:~/it117/hw/hw6$
#! /usr/bin/python3

import os
import sys

def get_environment_variable_value(variable_name):
   return os.environ.get(variable_name,'')

def get_dirs_from_path():
   path_var = get_environment_variable_value('PATH')
   directories = path_var.split(':')
   for entry in directories:
       print (entry)

get_dirs_from_path()
Note the way I change the get_environment_variable_value(variable_name). In your code there you will get KeyError if variable_name is not in the list of environment variables. This more or less removes the need of additional function...
I have to follow some steps ( mandatory ) to complete this task.


get_environment_variable_value()
  • The header for this function must be
    get_environment_variable_value(variable_name)

  • This function must accept a shell variable as its only parameter

  • The function must return the value of this shell variable

  • If the variable is not defined, the function must return the empty string
get_dirs_from_path()
  • The header for this function must be
    get_dirs_from_path()

  • This function must accept no parameters

  • This function returns a list of the directories contained in PATH
get_file_count()
  • The header for this function must be
    get_file_count(dir_path)

  • This function must accept a directory name as a parameter

  • The function must return the number of files in this directory

  • Sub-directories must not be included in this count
and the test code has to look like this :
path_dirs = get_dirs_from_path()
dir_count = get_file_count_for_dir_list(path_dirs)
print_sorted_dictionary(dir_count)
Buran already posted first two functions. And last one could be:

import os

def get_file_count(dir_path):
    count = 0
    for name in os.listdir(dir_path):
        if os.path.isfile(os.path.join(dir_path, name)):
            count += 1
    return count
Its possible to do it like one-liner with comprehension, but that would be little confusing.
(Mar-10-2017, 02:29 PM)valerydolce Wrote: [ -> ]What should be the "some_env_var_name" in this case ?
This is variable_name. Mine is just a discription as a var name.
Hi Zivoni,

He used the 'PATH' value instead of a variable under function  get_dirs_from_path()
(Mar-10-2017, 03:49 PM)valerydolce Wrote: [ -> ]I have to follow some steps ( mandatory ) to complete this task.
[snip]

and the test code has to look like this :


That smells like homework, so I've moved it.
That said, show us what you've tried, we won't do your work for you.
Nilamo,

It was a homework. If you read my initial post you'll see my work. However, i figured it out. 

Thanks for your help.
-Valerydolce