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 file?

    Pythonista
    2
    4
    1951
    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.
    • Dann239
      Dann239 last edited by

      So I had this working perfectly with just strings and a list. Switched to implementing writing to a file (tiny bit more permanent). All that's missing from this is the implementation of the string and complete implementation of the file writing with property settings AND the global value to pass my "inp()" around.

      My question is how to implement file writing and reading. I understand the general idea as I've seen a ton of examples. I however, can not get it to work. I don't even know how to prove the txt exists or not through python. So I'm calling on the amazing pythonista community to help a noob!!

      <code>
      import console
      import os

      def inp():
      open('list.txt', 'r')
      print list(list)
      print "-" *11
      for line in list:
      print line
      s = raw_input().title()
      if s == "Q":
      start()
      return

      def view():
      console.clear()
      print("Viewing Data")
      inp()
      return

      def add():
      console.clear()
      print("Adding Data")
      inp()
      open('list.txt', 'a').write(s.title())
      add()

      def edit():
      console.clear()
      print(" Remove?")
      inp()
      if s in list:
      list.remove(s)
      edit()
      else:
      edit()

      def start():
      console.clear()
      choice = ["[A]dd to list" , "[V]iew List" , "[E]dit List"]
      print("List App")
      print('\n'.join(choice))
      s = raw_input().title()
      if s == "ValueError":
      start()
      elif s == "A":
      add()
      elif s == "V":
      view()
      elif s == "E":
      edit()
      else:
      start()

      start()

      </code>

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

        One possible implementation: https://gist.github.com/cclauss/6656495

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

          Updated to refactor and add writeListToFile().

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

            Wow!! You don't know how much I appreciate that! I'm going to be studying this for a while. THANK YOU THANK YOU!!

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