Search code examples
pythonmodulewritetofile

Writing to a file from a python module


I am currently trying to debug an app that I have not written myself. I have narrowed the problem down to a particular method that is imported from a module outside of the current script. I would like to step through this module by writing to a file at each step, however it doesn't seem to work.

I have a version of the app that is running correctly and when I write to a file from within the module, the script runs fine but no file is created. Am I missing something here?

Example

Script that I am debugging

from module import method

example code ...
method(data)   ---  where error occurs
more code ...

module.py

def method(data):
    file = open('filetowrite.txt','w')
    file.write('something ....')
    file.close()

Solution

  • Do not reinvent the wheel, use standard logging module.

    Since you mentioned "I would like to step through this module" that's how you can do it with again standard pdb module.