0

I am trying to create a log file while running a python script, I am really not sure where the fault can occur so I can't put try and except here.

Giving you an example if my code says:

print "hello"

it should save a file on given location with .log/.txt file which will write the error like:

  File "<ipython-input-4-2a0eaa89f43f>", line 1
    print "hello"
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?

I tried to read the logging feature in python but I think that tells you how to write your own error message. Normally I write long python script to run, it run fine and when I put this in window scheduler and if some reason it fails, I don't understand why. So if there is a file getting genrated for these "python terminal error messages" as mentioned above, that will be pretty helpful. Tried to find everywhere and could not find something about this...

  • 1
    If you're on Linux, you could [redirect output to a file](https://stackoverflow.com/questions/6674327/redirect-all-output-to-file-in-bash) using your shell. Windows should also be able to do this – ForceBru May 05 '21 at 16:14
  • `stdout` and `stderr` redirecting works similarity on Windows as you can see here: https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file – jonathadv May 05 '21 at 16:19
  • not sure how this will work, I saw the link, not getting a clue. Can you help me with an example – deepankar srigyan May 05 '21 at 16:45
  • Perhaps you can put your code in an try statement, then for the except statement do something like – Ulto 4 May 05 '21 at 16:48

1 Answers1

0

Perhaps you can put your code in an try statement, then for the except statement do something like

except Exception as e: 
  files = open('filepath','w')
  files.write(e)
  files.close()
Ulto 4
  • 368
  • 4
  • 16
  • Though, it will give me one line exception result, dont give me the whole log as I am looking forward. Is there way I can get whole log. – deepankar srigyan May 07 '21 at 16:22
  • The only way I can think of is to subsection your code with these statements. Then after writing an error pass. – Ulto 4 May 07 '21 at 16:58