question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yaml.dump appends a line at the end of the file

See original GitHub issue

Hello,

yaml.dump() adds a new line at the end of a file. here an example to illustrate my problem.

test.yaml

foo:
  bar: hello
  loul: world

No new line after loul: world

from pathlib import Path

import yaml

def read_text():
    with open("test.yaml") as file:
        data = yaml.safe_load(file)
    
    with open("test2.yaml", mode="w") as file:
        yaml.safe_dump(data, file, default_flow_style=False, sort_keys=False)

    file_text = Path("test.yaml").read_text()
    new_file_text = Path("test2.yaml").read_text()
    
    assert file_text == new_file_text
    # Raise AssertionError

if __name__ == '__main__':
    read_text()

test2.yaml

foo:
  bar: hello
  loul: world
# this line is added

Is this correct ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
perlpunkcommented, Jan 30, 2020

YAML files with missing final linebreaks are actually valid. It’s just that PyYAML does not keep this information (or any other spaces or comments) when loading.

0reactions
NargiTcommented, Jan 30, 2020

Is there a way to raise an error at load time to verify if the original file is no valid from yaml point of view?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to indent lines when appending to existing yaml file with ...
Instead instantiate a YAML instance and use its dump method. The default ( round-trip parser) uses block-style for new elements yaml = ruamel....
Read more >
yaml.dump() appending to file rather than overwriting
yaml.dump() appending to file rather than overwriting how do i make set 'hi' to 1 and 'hello' to 1 import yaml class myClass():...
Read more >
Examples — Python YAML package documentation
Basic round trip of parsing YAML to Python objects, modifying and generating YAML: ... newline at the end # print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.
Read more >
PyYAML Documentation
yaml.dump supports a number of keyword arguments that specify formatting details for the emitter. For instance, you may set the preferred intendation and...
Read more >
YAML: The Missing Battery in Python
Choose the Dumper Class; Dump to a String, a File, or a Stream ... pure Python dumper appends optional dots at the end...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found