How to save a log file to Pythonista file system
How can I save a log file to my pythonista file system?
dgelessus last edited by dgelessus
You can use the normal Python
openfunction, like you would with a normal Python installation on a regular computer. (For details on how to use
open, see the Python standard library docs, section "Built-in Functions". Most Python tutorials and beginner's books will also explain how
By default, all file names are relative to the folder that your script is in, so if you use
open("mylog.txt", "w"), a file
mylog.txtwill be created in the same folder that the script is in. If you want to always use a fixed path, you can use something like
~/Documentsis the "Library" that you see in Pythonista's file list, so this will create a file
mylog.txtin the folder
Hmmm. Tried that and it didn't seem to work. No file showed up.
dgelessus last edited by
Can you post your code? Otherwise it's difficult to tell what might be going wrong.
Simple enough. Logfile_out is set to "log.txt"
if self.logfile_out is not None: self._of = open(self.logfile_out, mode='w', encoding='utf-8')
It has to be something stupid.
ccc last edited by
Is self.logfile_out a file name or a file pointer? The last line seems to indicate that it is a file name (i.e. a string). An empty string '' != None. This is the sort of thing that typechecking will help with in Python 3.7 :-) (see the videos from the recent Pycon).
My recommendation is that you rename self.logfile_out to self.log_file_name to keep things clear. I would rewrite as:
if self.log_file_name: self._of = open(self.log_file_name, mode='w', encoding='utf-8')
It was something stupid. Spelled the reference to the file Handel wrong.