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.


    Writing to a File with python in editorial

    Editorial
    4
    10
    7571
    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.
    • robopu
      robopu last edited by

      i wasn't sure whether to put this in the pythonista forum but i've been working solely in the editorial python console so it's mostly a python editorial question.

      so i've been able to do some "good stuff" with the python console in editiorial.. i've managed to generate a lot of text that i mostly just print to the console. but i would like to be able to send that output programmatically to a new document in my local folder.

      i checked the online documentation in the console and searched a bunch online today but am not finding the info i need. i was wondering if anybody could point me in the right direction to do (any of) the following

      keep in mind i'm on the road and i have to run editorial on my aging handheld (ie i don't have access to editorial on an ipad or a mac any time soon!)

      1. i obviously need to create a new file in editorial using python, so i'm wondering how to do that? since i've programmed this thing in code from the start, i'd really love not having to jump back with any workflow integration. that way everything is self-contained in my script file

      fwiw, it looks like i might be able to access a document that is currently open but i don't want to have to do that b/c it breaks the rule about the script containing all the functionality by itself / inherently.

      1. once i get a file pointer in #1, i can write to it. but i'm trying to figure out the best way to do this.

      as of now my script is already written and uses simple print statements to send TONS of output beautifully to the console screen.

      so i'm thinking:

      2a) if i can redirect console output so that it goes to both console AND the file pointer that might be simplest. kind of like you do on the linux / unix command line

      but that might not be possible so..

      2b) i'm wondering if there is some other way to capture / redirect all that print output to a single variable simply by directing the console out to a variable using some python construct. i think i've done this in the past with other languages but i forgot what it's called.

      otherwise, i think i'm just going to have to rewrite some of my code to concatenate all that text using variables inline in the code where i currently have print statements which is going to require some extra refactoring.

      i guess i'm not sure if i can just append LOTS of text to a simple text variable over MANY iterations or i need to store the text generated in the each iteration of the loop in an array.

      in sum, i'm looking for the simplest way to do #1 and #2 with as little refactoring in my script as possible

      1. as i'm generating the text in my script, web content p tags are currently converted to text on the fly which leave no double spacing between paragraphs in the text. so i'm wondering what the best way in python is to replace all single new line / paragraph breaks in the text contained in my content variables with two new lines

      i hope that all makes sense.

      any help is greatly appreciated.
      thx

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

        editor.get_file_contents(name[, root])
        Return the contents of a file in the document library. root can be either ‘local’ or ‘dropbox’. The default is ‘local’.

        Note that this returns a plain byte string. If you want to get the text of a file that contains unicode, you have to decode it yourself (Editorial saves text files as UTF-8).

        editor.set_file_contents(name, new_contents[, root])
        Writes new data to the file with the given name (a relative path). If a file with this name exists, it is overwritten.

        Note that new_contents is a byte string, which means that you can also use this to write binary files (such as images), but if you write non-ASCII text, you have to encode it yourself.

        root can be either ‘local’ or ‘dropbox’. ‘local’ is the default.

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

          hey josh thx

          fwiw, i actually read that same info in the documentation earlier today.. BUT.. it doesn't tell me whether that call will create a NEW document or not. it surely opens an existing one. i want to always create a new one

          and unfortunately that unicode thing went over my head a bit. i've got plain old vanilla text i want to write to the file. if i copied the console output and pasted it back into a new document manually, it would just work. but i'm not sure if when i put the text (ascii i'm pretty sure???) out to file if i need to encode it and this could be a lot of text.. like the size of a novel to be exact by the time it's all generate.

          thx

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

            In Python, you can always create a new file by writing:

            with open('my_cool_file.txt', 'w') as out_file:
                out_file.write('My cool message...')
            
            1 Reply Last reply Reply Quote 0
            • robopu
              robopu last edited by

              ccc

              fyi, i found that basic python write call the other day. similar to most write calls

              BUT.. it doesn't tell me whether it will actually work and how they will work specifically in iOS and the applications running them.

              for instance, i'm using the editorial python console but to my knowledge there is no filesystem there to write to otherwise i'd be able to save multple .py files there but that doesn't seem to be the case.

              the vanilla python call doesn't tell me if i'm going to be allowed to write to my editorial folders / locations (remote, local or otherwise) either,

              i haven't backed up or synchd anything in my editorial files system yet so before i start playing around with system write calls, i want a little more clarity and first hand experience of using the suggestions presented here to actually write a new file to editorial if possible. i hope that's a little more clear now that i've responded to both replies on this thread

              fwiw, i'd already seen both recommendations by posters so far before i typed my original post.

              thx for sharing regardless

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

                [the Python documentation] doesn't tell me whether it will actually work and how they will work specifically in iOS and the applications running them.

                Agreed. but this will become clear after some trial and error (which I agree can be somewhat scary when you are not backed up).

                To my knowledge there is no filesystem there to write to.

                There is indeed a file system to write to. The iOS is really just Darwin unix that is well hidden but all the power of a real Unix is available under the hood. This all works (and will not harm any of your files ;-) ):

                import os
                print(os.getcwd())  # https://docs.python.org/2/search.html?highlight=os.getcwd#os.getcwd
                print(os.listdir(os.curdir))  # https://docs.python.org/2/library/os.html?highlight=os.listdir#os.listdir
                print(os.listdir(os.pardir))  # https://docs.python.org/2/library/os.html?highlight=os.curdir#os.curdir
                

                Your files are in that Unix file system

                the vanilla python call doesn't tell me if i'm going to be allowed to write to my editorial folders / locations (remote, local or otherwise) either,

                Agreed. but this too will become clear after more trial and error.

                i haven't backed up or synchd anything in my editorial files system yet

                Eeekkk!

                so before i start playing around with system write calls, i want a little more clarity and first hand experience of using the suggestions presented here

                Cool. Focus for now on os.getcwd(), os.listdir(), os.chdir() and read(). These will not modify your files but should help you to understand that there is a filesystem down there and that you can move around in it to find and read your Editorial files. Only after you are super comfortable with these should you consider going back to write(). I used the hack https://github.com/cclauss/Ten-lines-or-less/blob/master/cd_ls_pwd.py when I was learning my way around.

                to actually write a new file to editorial if possible.

                Very possible... You are close to finding the way.

                1 Reply Last reply Reply Quote 2
                • robopu
                  robopu last edited by

                  thx for input ccc. it definitely helps.

                  i guess i'll "play around" out there :) i was hoping i wasn't going to have to though ;)

                  fwiw, i have plenty of paid linux/unix experience. the thing is i have little iOS system experience and i'm rather new at python though the latter isn't as big a deal.

                  i figured that including a python console in an editor app whose main objective is to read and write files to what appears to be a very defined and single location in a file system, would mean that some examples and or documentation on writing to that local folder natively in python would be generally available. ;) i guess i shouldn't assume or presume though :)

                  i'll add that googling on the terms "editorial" and "python" plus whatever you're actually searching to do in editioral with python is often a long, labor-intensive process with little results due to the generic app name and search term "editorial" :)

                  it sounds like there's a lot i can do with python at the system level in iOS though which is very cool. it pays to be careful though. i just don't want to invest a lot of time in the iOS side of things. at least not right now nor do i want to shoot myself in the foot accidentally. i just want to create a new file in root of editorial's local files folder and output to it and close the file.

                  i do appreciate the advice though. it can get me what i need in due time.

                  thx

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

                    hey ccc would you have any input on questions #2 or #3 in my OP.?

                    #2 being if i can redirect output console output to a file like in linux either programatically in the script itself OR by redirecting the script on the console command line. the thing is i'm not sure how to run a script from the command line in editiorial's python console.. since the only way i know how to run it is to hit the run icon from the script editor pane itself

                    thx

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

                      @robopu You can read from sys.stdout and copy that to a file.

                      FWIW, you can do most of everything you can do in "vanilla" Python in Editorial / Pythonista. Installing modules in Editorial is difficult, but the Python consoles in @omz's apps aren't re-implementations, it's the actual Python binaries compiled for iOS.

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

                        cool thx again.. finally bought pythonista.. will be exploring in due time..

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