0

I am new to Python. I came across a weird case and am not able to figure out what the issue is. I have 2 versions of a function written in Python :-

v1 -

def fileLookUp(fixedPath, version):
    if version:
        targetFile="c:\\null\\patchedFile.txt"
    else:
        targetFile="c:\\null\\vulFile.txt"

#some more code follows

and v2 -

def fileLookUp(fixedPath, version):
    if version:
        print "ok"
    else:
        print "not ok"

#some more code follows

where the parameter fixedPath is a string that is entered and the parameter version is supposed to be an integer value. The 1st function (v1) does not work as expected, while tje second works perfectly. Both the times the function is called as fileLookUp("c:\\dir\\dir\\", 1).

In the 1st case the error received is :-

fileLookUp("D:\\Celine\\assetSERV\\", 1)
Exception: fileLookUp() takes exactly 2 arguments (1 given)

Please let me know why is the 1st function throwing the exception?

Here is the actual code....

from System.IO import *;

def fileLookUp(fixedPath, version):
if version:
    targetFile="c:\\null\\patchedFile.txt";
else:
    targetFile="c:\\null\\vulFile.txt";
vulFileHandle=open(targetFile,"a+");
temp=fixedPath;
if not Directory.GetDirectories(fixedPath):
    files=Directory.GetFiles(fixedPath);
    for eachFile in files:
        print eachFile;
        hash = Tools.MD5(eachFile);
        print hash;
        vulFileHandle.write(eachFile+'\t'+hash+'\n');
else:
    directory=Directory.GetDirectories(fixedPath);
    for folder in directory:
        if vulFileHandle.closed:
            vulFileHandle=open(targetFile,"a+");
        fixedPath="";
        fixedPath+=folder;
        fixedPath+="\\";
        vulFileHandle.close();
        fileLookUp(fixedPath);
    filess=Directory.GetFiles(temp);
    for eachFilee in filess:
        if vulFileHandle.closed:
            vulFileHandle=open(targetFile,"a+");
        print eachFilee;
        hashh = Tools.MD5(eachFilee);
        print hashh;
        vulFileHandle.write(eachFilee+'\t'+hashh+'\n');
if not vulFileHandle.closed:
    vulFileHandle.close();

it is simply a recursive code to print out the hash of all files in a directory.

11
  • Fix code formatting, pls. Your variant will fail with IndentationError. Commented May 14, 2013 at 15:00
  • Well i guess there is no indentation error as i have used Notepad++ to code it. So am pretty sure that the indentation is as required. Commented May 14, 2013 at 15:01
  • SO doesn't format tabs the same as spaces. I've edited, just awaiting peer review Commented May 14, 2013 at 15:02
  • if there were any indentation errors, an IndentationError should have been reported as said by @AlexeyKachayev, which is not the case. Commented May 14, 2013 at 15:04
  • Please show us the code that actually calls these functions and raises the error, and the full stacktrace. It's good practice to post a full, short, self-contained example that demonstrates your problem. Commented May 14, 2013 at 15:04

3 Answers 3

3

You have a call "fileLookUp(fixedPath);" around line 26 or so (just counted roughly) with only one argument sent in. Your definition doesn't allow that. Send in the version in this call, or give a default value to version in the definition.

Sign up to request clarification or add additional context in comments.

1 Comment

oh... ok ok .. how silly of me... its the recursive call that's the source of the Exception. Thanks
1

The way these functions are written, both of them must be called with two arguments. The error message you're getting indicates that one of them is being called with only one argument. In Python, if you want to make an argument optional, you have to explicitly state what value the optional argument should have if it is not provided. For example, since version is to be an int, and you test it with if version:, a good default value could be 0. You could also use None.

def fileLookUp(fixedPath, version=0):
    # etc.

def fileLookUp(fixedPath, version=None):
    # etc.

If 0 is a valid version number, and you want to test for whether a value was actually passed, use the second and test against None specifically:

def fileLookUp(fixedPath, version=None):
    if version is None:
        # etc.

2 Comments

but my second argument is not really optional. both the arguments are compulsory.
Well then, call it that way. :-)
1

In the (very badly formatted) full exemple, you're making a recursive call to fileLookUp not passing the version argument.

Comments

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.