Python Forum
How to define a global file? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to define a global file? (/thread-298.html)

Pages: 1 2


How to define a global file? - dullboy - Oct-05-2016

I need to set up a file as a log file in my python program. So basically I need to create a global file. I am just wondering what is the best way to define a global file in python? Thanks.


RE: How to define a global file? - wavic - Oct-05-2016

I don't get you what you mean with the fraze 'a global file'. With Python you can read and write any file as long as you have permissions to do that.


RE: How to define a global file? - Larz60+ - Oct-05-2016

Logging is a fairly complex subject. Where you declare the file, and open it is very much dependent on how your code is structured.
the easiest thing to do is go to https://pypi.python.org/ and query 'logging' find a suitable module, install it and just import
into your code.


RE: How to define a global file? - dullboy - Oct-05-2016

Actually, all I want is to trace the program execution and list the functions executed during the program run. Should I simply use trace module? Thanks.


RE: How to define a global file? - Skaperen - Oct-05-2016

in some cases i cheat to do logging.  i open() or os.open() a file and os.dup2() its file descriptor to stdout (1).  i am running Linux.  i have no idea if this works in Windows (nor do i care).  then all print() goes to that file, even in called functions and forked processes (which i do a lot of).


RE: How to define a global file? - wavic - Oct-05-2016

I am running linux too. @scaperen, you say that all output from a program can be wrote in a file? Which os. method is doing that?


RE: How to define a global file? - j.crater - Oct-05-2016

I once wanted to output data from top to a file, so I used this
top -n 10 -b > top-output.txt
to run the process.


RE: How to define a global file? - wavic - Oct-05-2016

Output redirection is one thing I know well but I was asking how to do it from Python script. I will look at os module later to see what is provided.


RE: How to define a global file? - j.crater - Oct-05-2016

Oh sorry then wavic, I didn't catch that bit. I will be glad to hear what you found out if you will look into it.


RE: How to define a global file? - wavic - Oct-05-2016

Actually that is crossed my mind after @scaperen's post. Post #5