![]() |
Calling a Returned Value to Another Function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Calling a Returned Value to Another Function (/thread-2363.html) |
Calling a Returned Value to Another Function - valerydolce - Mar-10-2017 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. ![]() RE: Calling a Returned Value to Another Function - wavic - Mar-10-2017 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) RE: Calling a Returned Value to Another Function - valerydolce - Mar-10-2017 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:
RE: Calling a Returned Value to Another Function - buran - Mar-10-2017 #! /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... RE: Calling a Returned Value to Another Function - valerydolce - Mar-10-2017 I have to follow some steps ( mandatory ) to complete this task. get_environment_variable_value()
path_dirs = get_dirs_from_path() dir_count = get_file_count_for_dir_list(path_dirs) print_sorted_dictionary(dir_count) RE: Calling a Returned Value to Another Function - zivoni - Mar-10-2017 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 countIts possible to do it like one-liner with comprehension, but that would be little confusing. RE: Calling a Returned Value to Another Function - wavic - Mar-10-2017 (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. RE: Calling a Returned Value to Another Function - valerydolce - Mar-10-2017 Hi Zivoni, He used the 'PATH' value instead of a variable under function get_dirs_from_path() RE: Calling a Returned Value to Another Function - nilamo - Mar-22-2017 (Mar-10-2017, 03:49 PM)valerydolce Wrote: I have to follow some steps ( mandatory ) to complete this task. 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. RE: Calling a Returned Value to Another Function - valerydolce - Mar-28-2017 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 |