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.


    yaml.dump() appending to file rather than overwriting

    Pythonista
    help needed help dump yaml yml
    5
    8
    5628
    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.
    • Sparky01-
      Sparky01- last edited by Sparky01-

      yaml.dump() appending to file rather than overwriting
      how do i make set 'hi' to 1 and 'hello' to 1

      import yaml
      
      class myClass():
        def __init__(self):
          self.configFile = open('config.yml', 'r+')
          self.config = yaml.load(self.configFile)
          self.config['hi'] = 1
          self.config['hello'] = 1
          yaml.dump(
            self.config,
            self.configFile,
            default_flow_style=False
          ) 
          print(self.config)
          # returns {'hi': 1, 'hello': 1} 
          
      myClass()
      

      config file before:

      hi: 0
      hello: 0
      

      after:

      hi: 0
      hello: 0
      hi: 1
      hello 1
      

      what i want:

      hi: 1
      hello: 1
      
      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by ccc

        TL;DR:

        1. If you open() a file then you should close() it or even better, use with open as which will autoclose the file.
        2. Opening files in r+ mode is complicated.
        c = {"hi": 0, "hello": 0}
        d = {"hi": 1, "hello": 1}
        # print(c | d)  # Pythonista's version of Python is too old
        c.update(d)
        print(c)
        
        
        1 Reply Last reply Reply Quote 0
        • Sparky01-
          Sparky01- last edited by

          the python dictionary is fine and printing what i want, printing the dict prints
          {'hi': 1, 'hello': 1}
          but when i dump it puts the yaml at the end of the file rather than overriding the file

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

            @Sparky01

            I haven't tested it, but I think the problem is that you aren't using file offsets correctly. Basically, right now you are reading the file, then continue writing from the last position you have read.

            The best solution would be to seperate reading and writing the files.

            import yaml
            
            class MyClass():
              def __init__(self):
                with open('config.yml', 'r') as fin:
                    self.config = yaml.load(fin)
                self.config['hi'] = 1
                self.config['hello'] = 1
                with open('config.yml', 'w') as fout:
                    yaml.dump(
                      self.config,
                      fout,
                      default_flow_style=False
                    ) 
                print(self.config)
                # returns {'hi': 1, 'hello': 1} 
                
            MyClass()
            

            Alternatively, call self.configFile.seek(0) after reading. This sets the read/write offset to the beginning of the file. Be warned that this only works if the new file content has at least the same length as the original file content, otherwise trailing bytes will remain!

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

              In a situation like this I would never write to the same file I read from.:

              1. read from the config file.
              2. Write to a new file.
              3. Rename the old config file to something like ..._bak.yml.
              4. Rename the new file to the name of the old config file.

              My 2cts.

              cvp 1 Reply Last reply Reply Quote 1
              • cvp
                cvp @halloleooo last edited by cvp

                @halloleooo between 2 and 3: remove the previous bak file

                halloleooo 1 Reply Last reply Reply Quote 0
                • halloleooo
                  halloleooo @cvp last edited by

                  @cvp Only when you use os.rename. If you use pathlib's Path.rename I think it silently overwrites the old backup file.

                  cvp 1 Reply Last reply Reply Quote 1
                  • cvp
                    cvp @halloleooo last edited by cvp

                    @halloleooo Ho. Nice, I didn't know, never too old to learn

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