Search code examples
pythondirectoryargs

Calling a function with an uninitialised variable that I want to convert to a string


I'm a little stuck with what is probably a simple problem. There are probably several ways to resolve this issue so I thought I'd open it up to stackoverflow to suggest some different approaches. My code is below:

import os


root_directory = ('C:/path/to/files/')


def directories(*dirs):
    for directory in dirs:
        print(os.path.join(str((root_directory, directory))))


directories(inbox, test)

I want this to return:

('C:/path/to/files/inbox')
('C:/path/to/files/test')

but I'm getting a traceback:

Traceback (most recent call last):
  File "C:/path/to/files/python_file.py", line 24, in <module>
    directories(inbox, test)
NameError: name 'inbox' is not defined

I don't want to to call the function with the arugments as strings, i.e. directories('inbox', 'test')

I'm trying to create this function that will look in a variable number of sub-directories, based on how the function is called. So I might call directories(inbox) and it will look in the inbox folder, or i might call directories(inbox, test) and it will look in both the inbox and test folder.

Any help / suggestions would be much appreciated!


Solution

  • This will give you desired output: This is final output which you can get

    import os
    root_directory = ("C:/Users/vjadhav6/Desktop/")
    def directories(*dirs):
        for directory in dirs[0]:
            print(os.path.join(str(root_directory) , directory))
    
    d = {"inbox":"inbox","test":"test"}
    directories(d.values())
    

    OR

    import os
    root_directory = ("C:/Users/vjadhav6/Desktop/")
    def directories(*dirs):
        for directory in dirs[0]:
            print(os.path.join(str(root_directory) , directory))
    inbox = "inbox"
    test = "test"
    directories(inbox, test)