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.


    Opening file for append ISSUE

    Pythonista
    5
    7
    3273
    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.
    • Oak
      Oak last edited by

      Hi all, I have an issue that I absolutely cannot get around. When I run my script in the app, it works perfectly fine. Everything is normal. When I call my app through Safari it still runs normally, with the exception that it refuses to append to another file.

      So to be clear: the only problem is when accessing the closed app using Pythonista://CoordinatesGet.py?action=run it doesn't write to my file. It DOES write to my file if I call it again, with the app already open, and it DOES write to my file when running the program normally from within the app. Here is my code:

      <code>
      theFile = open('CoordinatesList.txt','a')
      import location
      location.start_updates()
      myCoords=location.get_location()
      location.stop_updates()
      longitude=str(myCoords['longitude'])
      latitude=str(myCoords['latitude'])

      theFile.write('\n')
      theFile.write(longitude)
      theFile.write('\n')
      theFile.write(latitude)

      print 'Coordinates:',latitude, longitude
      </code>

      I really appreciate the help! I'm starting to think it's a bug.

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

        As far as I can tell your code never actually flushes (write to disk) or closes the file. This means that any changes will not be visible until theFile is removed (which happens when you run a new script, because then Pythonista will wipe the Python environment by default). When working with files it's usually best to use the with statement, which will ensure that the file is always properly written and closed:

        with open("some_file.txt", "a") as f:
            f.write("some text\n")
            # ... any code that uses the file (f) ...
        
        print("Done!")
        

        Note that the file object (f) can only be used inside the with block. Once that block is exited, f becomes unusable. This means that any code that uses f needs to be in the with block.

        (By the way, see this link on how to post code blocks on the forums.)

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

          Not sure if this what you after @Oak. I am very new, but still trying to help as many help me. So take it with a grain of salt

          
          import location, os.path
          
          __FILE_DIR__ = os.path.expanduser('~/Documents/')
          __FNAME__ = 'CoordinatesList.txt'
          
          __MY_FILE__ = __FILE_DIR__ + __FNAME__
          
          if not os.path.exists(__MY_FILE__):
          	theFile = open(__MY_FILE__, 'w')
          	is_new_file = ''
          else:
          	theFile = open(__MY_FILE__, 'a') 
          	is_new_file = '\n'
          	
          location.start_updates()
          myCoords=location.get_location()
          location.stop_updates()
          longitude=str(myCoords['longitude'])
          latitude=str(myCoords['latitude'])
          
          theFile.write(is_new_file)
          theFile.write(longitude)
          theFile.write('\n')
          theFile.write(latitude)
          
          theFile.close()
          
          
          1 Reply Last reply Reply Quote 0
          • polymerchm
            polymerchm last edited by

            I thinks the issue might be not closing the file across calls.

            try:

            import location,time
            
            location.start_updates()
            mycoords = location.get_location()
            location.stop_updates()
            
            with open('coordinates.txt','a') as fh:
                 fh.write(str(mycoords)+'\n'+str(time.ctime())+'\n')
            
            

            This worked fine for me, but did keep jumping back to pythonista rather than remaining in Safari. Also to better format your code for markdown, use the following approach:

            ```python

              your code snippet here
            

            ```

            in your posts to the forum

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

              Thank you all, you were most certainly right! File was not closing. I have fixed it now by simply using <code> with open </code> for the file.

              Thanks again!

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

                Talk about great minds think alike!!!

                1 Reply Last reply Reply Quote 0
                • galewinston
                  galewinston last edited by ccc

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors