1

I have a function called MyScript() that I have defined in a file called MyScript.py. MyScript takes several arguments that I would like to prompt the user in the command line to enter. So I wrote a separate function RunMyScript() defined in RunMyScript.py to first run in the command line to gather the arguments:

def RunMyScript():
  import os
  dir1 = input('Enter the Directory1 file path: ')
  dir2 = input('Enter the Directory2 file path: ')
  dir3 = input('Enter the Directory3 file path: ' )
  flag = input('Flag? (Y/N): ')

os.chdir = dir1
from MyScript import MyScript
MyScript(dir2,dir3,flag)

And then generically the MyScript function begins:

def MyScript(dir2,dir3,flag="N"):
    import os

    os.chdir(dir2)
    if flag=="Y":
    DoSomething
    wb.save(dir3)

When I try to run RunMyScript in the command line I get the error "TypeError: 'str' object is not callable. I suspect it has something to do with how it interprets the flag input Y or N, but I'm not positive.

I am new to Python and the site so please pardon me if the question is not phrased properly, I will provide more detail as needed, and I have searched the site for similar questions but it's not clear that I'm having the same problem as others in the past.

Thank you.

4
  • What line is the error occurring on? What are the contents of the line? Commented Aug 10, 2015 at 22:35
  • Have you tried add single quotes to the input? Commented Aug 10, 2015 at 22:39
  • After making the change @Kevin suggested below, I've now narrowed it down to the Flag arg and I think how it's being passed through to MyScript. Think because the user input from RunMyScript "Y" or "N" is being interpreted as a string, it fails when trying to call in MyScript. So I need to convert to a variable. Will post answer if that works Commented Aug 11, 2015 at 2:25
  • My interim solution feels cheap but basically I just added lines to RunMyScript to say 'if Flag = N Flag =1 elif Flag = Y Flag = 2' and then passed it through to MyScript as an int not a str. Leaving the post open though as others may have better answers that could be useful to others Commented Aug 11, 2015 at 2:42

2 Answers 2

1

I'm not entirely sure what you are attempting to do but I would think your problem has to do with the line :

os.chdir = dir1

You are changing os.chdir to a string. To properly change the directory you should use:

os.chdir(dir1)
Sign up to request clarification or add additional context in comments.

1 Comment

This is part of it thank you. To your broader point - I'm attempting to use the Run function to give the user an easier/prompted way to assign the needed directory arguments for MyScript (specific to their environment) in the command line, before MyScript executes. Thought being I can do some error handling up front as well - so if they enter the directory wrong. There may be a better overall way to do that, for instance I've seen some references to the "sys" module searching this site, but wasn't sure if it applied.
0

In MyScript function you call os.chdir(dir2) but os.chdir is a string and it's not callable. Try os.chdir = dir2 instead.

EDIT : It's the opposite... I'm so confused.

But now, didn't you have a scope issue ? When I copy-paste your code, it doesn't work because os.chdir(dir1) isn't in the same scope that import os.

I suggest you to move all the import to the top of script and change input in raw_input to a best support of string input.

Try to change your code like that :

import os
import MyScript

def RunMyScript():
    dir1 = raw_input('Enter the Directory1 file path: ')
    dir2 = raw_input('Enter the Directory2 file path: ')
    dir3 = raw_input('Enter the Directory3 file path: ' )
    flag = raw_input('Flag? (Y/N): ')

    os.chdir(dir1)
    MyScript(dir2, dir3, flag)

if __name__ == '__main__':
    RunMyScript()

and for MyScript :

import os 

def MyScript(dir2, dir3, flag = "N"):
    os.chdir(dir2)

    if flag == "Y":
        #Do Something

    wb.save(dir3)

I hope that solve your problem.

6 Comments

According to the documentation, it os.chdir() is certainly a function.
That was definitely part of the issue as @Kevin raised above, should be: os.chdir(dir1) in RunMyScript silly since I did it right the second time
@BJBeans I changed my answer. Hope it will help.
Thank you! What is the thought behind the if name == 'main': I've been reading about it elsewhere here but unsure how it applies in my specific case
In your case, if the script is the main script (not an import) RunMyScript () is automatically called. So if you import your script in an other script RunMyScript () is not automatically called. You can read the condition like "if this script is the main script do this.."
|

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.