Hi, I am fairly new to Python. I am familiar with the concept to pass data accross functions.

In theory,

def c():
   r = raw_input("Ask Something? ")
   ..
   return r

def p(x):
    ...
    do something

r = c()
p(r)

The code below works just fine via Terminal ( python filename.py file.txt ), but I want to add workflow where a variable stores the path to the file and passes it to the function ( processFile ). I just cant get the data / value passed to the function.

This is the code I am trying to edit :

def registerException(exc):
      exceptions[exc] += 1

def processFile(x):
  with open(x, "r") as fh:
    currentMatch = None
    lastLine = None
    addNextLine = False
    for line in fh.readlines():
      if addNextLine and currentMatch != None:
        addNextLine = False
        currentMatch += line
        continue
      match = REGEX.search(line) != None
      if match and currentMatch != None:
        currentMatch += line
      elif match:
        currentMatch = lastLine + line
      else:
        if currentMatch != None:
          registerException(currentMatch)
        currentMatch = None
      lastLine = line
      addNextLine = CONT.search(line) != None
    # If last line in file was a stack trace
    if currentMatch != None:
      registerException(currentMatch)

for f in sys.argv[1:]:
  processFile(f)

for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True):
  print item[1], ":", item[0]

It doesnt matter if I declare the variable as Global or local. Could someone please help me solve this issue?

Which value is not passed to the function ? Is it the call to processFile(x) ? Did you
try to add statements such as print(sys.argv[1:]) before line 21 or print(x) at the beginning of processFile() ? You could use my printat snippet to help you debug this https://www.daniweb.com/programming/software-development/code/479747/print-with-line-and-file-information

Some remarks

  • Configure you editor to indent python code with 4 space characters when you hit the tab key.
  • Testing against None is best done with if value is not None instead of if value != None
  • It seems to me there is a risk that statement at line 18 fails because lastline could be None.

I have made some changes to the original code, and now I am getting the following TypeError: 'NoneType' object is not iterable.

def c():
    path = raw_input("Path to file? ")
    r = os.path.abspath(path)

def process_file(filename):
    current = None
    last_line = None
    continue_line = False
    with open(filename, "r") as fh:
        for line in fh:
            if continue_line and current is not None:
               continue_line = False
               current += line
               continue
            if REGEX.search(line):
               if current is None:
                  current = last_line
               current += line
            else:
               if current is not None:
                  yield current
               current = None
            last_line = line
            continue_line = CONT.search(line)
        # If last line in file was a stack trace
        if current is not None:
            yield current

def process_files(filenames):
    exceptions = defaultdict(int)
    for filename in filenames:
        for exc in process_file(filename):
            exceptions[exc] += 1

for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True):
    print item[1], ":", item[0]

r = c()
process_files(r)

I have removed sys.argv[1] since it requires an argument at the command line when running the script.

I think the new error I am getting is due to the OS Path. How can I fix this ?

You need at least a return r statement in c(). Also, if you have more errors, please post the whole exception traceback. There are other errors: process_files() expects a sequence of file names, but c() can produce only one file name, you could call process_files([r]) for example.

Thank You both !!! I finally got it working with your suggetions !!! I was missing process_files([r]) !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.