joeyaurel / python-gedcom

Python module for parsing, analyzing, and manipulating GEDCOM files
https://gedcom.joeyaurel.dev
GNU General Public License v2.0
160 stars 39 forks source link

can't save gedcom file #27

Closed ogruenig closed 4 years ago

ogruenig commented 5 years ago

Hello, I am trying to read in a gedcom file, modify the data and then save a gedcom file. It works ok until I try to save it using save_gedcom() - I get only an empty output file ( 0 bytes). I assume I have to open a file using open( filename, "w"), then pass it to save_gedcom. However I get an empty file ( 0 bytes). Is this supposed to work? It does not even work when I try to save the "original", i.e. non-modified gedcom file, such as in the code below.

`file_in = "/home/xy/test1.ged" file_out = "/home/xy/test1_out2.ged"

from gedcom.element.individual import IndividualElement from gedcom.element.object import ObjectElement from gedcom.parser import Parser

gedcom_parser = Parser() gedcom_parser.parse_file(file_in)

outfile = open( file_out, "w" ) gedcom_parser.save_gedcom(outfile) outfile.close()`

KeithPetro commented 5 years ago

I believe that the culprit here is in to_gedcom_string()@element.py:275-276. The issue is that in save_gedcom() we are making the following call:

open_file.write(self.get_root_element().to_gedcom_string(True))

The ROOT element is level -1, therefore the following will result in to_gedcom_string() instantly returning an empty string:

if self.get_level() < 0:
    return ''

Edit: I've made a quick change that should fix this issue.