omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How to save a log file to Pythonista file system

    Pythonista
    file log save
    3
    7
    5223
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • OTPSkipper
      OTPSkipper last edited by

      How can I save a log file to my pythonista file system?

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by dgelessus

        You can use the normal Python open function, 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 open works.)

        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.txt will 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 open(os.path.expanduser("~/Documents/logs/mylog.txt"), "w"). ~/Documents is the "Library" that you see in Pythonista's file list, so this will create a file mylog.txt in the folder logs.

        1 Reply Last reply Reply Quote 1
        • OTPSkipper
          OTPSkipper last edited by

          Hmmm. Tried that and it didn't seem to work. No file showed up.

          1 Reply Last reply Reply Quote 0
          • dgelessus
            dgelessus last edited by

            Can you post your code? Otherwise it's difficult to tell what might be going wrong.

            1 Reply Last reply Reply Quote 0
            • OTPSkipper
              OTPSkipper last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • ccc
                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')
                
                1 Reply Last reply Reply Quote 1
                • OTPSkipper
                  OTPSkipper last edited by

                  It was something stupid. Spelled the reference to the file Handel wrong.

                  Thanks!!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors