0

I'm sure this is really simple but I can't figure out how to make a parsed result into its own file then move it to my desktop using python. Here is my code so far. I just want to save the result "names" as its own file then move it to my desktop but I can't find the answer anywhere. Is this an uncommon practice?

from gedcom.element.individual import IndividualElement
from gedcom.parser import Parser
import os
import shutil
import pickle


# Path to your `.ged` file
file_path = '/Users/Justin/Desktop/Lienhard_Line.ged'

# Initialize the parser
gedcom_parser = Parser()

# Parse your file
gedcom_parser.parse_file(file_path, False)

root_child_elements = gedcom_parser.get_root_child_elements()

# Iterate through all root child elements
for element in root_child_elements:

    # Is the `element` an actual `IndividualElement`? (Allows usage of extra functions such as `surname_match` and `get_name`.)
    if isinstance(element, IndividualElement):

        # Get all individuals whose surname matches "Doe"
        if element.surname_match('Lienhard'):

            # Unpack the name tuple
            (first, last) = element.get_name()
            names = (first + " " + last)
            
            pickle.dumps(names)
tripleee
  • 175,061
  • 34
  • 275
  • 318
Justin Benfit
  • 423
  • 3
  • 11
  • Where is the data now? – Tim Roberts Mar 23 '21 at 03:57
  • I believe that the data is saved to the names variable which I tried to pickle so I'm assuming it's inside the .py file that all of this code was taken from. When you pickle a file does it automatically save it to some other location? I'm not quite sure how that part works. – Justin Benfit Mar 24 '21 at 12:56

1 Answers1

1

Saving a file to one location and then moving it is to another not how it's usually done, no. Just save to the final location.

from pathlib import Path

pic = Path.home() / 'Desktop' / 'names.pickle'
with pic.open('w') as picklefile:
    pickle.dump(names, picklefile)

The pathlib library makes working with file names somewhat easier than the old os.path API, though both are in common use.

Writing and then renaming has some uses; if you need to absolutely make sure your data is saved somewhere, saving it to a temporary file until it can be renamed to its final name is a fairly common technique. But in this case, saving to a different location first seems like it would only introduce brittleness.

The above assumes that you have a directory named Desktop in your home directory, as is commonly the default on beginner-oriented systems. To be perfectly robust, the code should create the directory if it doesn't already exist, or perhaps simply save to your home directory.

Indeed, a much better convention for most purposes is simply to always save in the current directory, and let the user take it from there. Hardcoding paths in another directory just adds confusion and uncertainty.

with open('names.pickle', 'w') as picklefile:
    pickle.dump(names, picklefile)

This code creates a file called names.pickle in the invoking user's current working directory. The operating system provides the concept of a current working directory for precisely this purpose. Perhaps see also Difference between ./ and ~/ if you need more details about how this works.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you for this! So you're saying that best practice is to save the file in the current directory? How is that done exactly? Or is that the result that your above solution creates? Is there a way to print the path of a variable (like my "names" variable from above?) – Justin Benfit Mar 24 '21 at 12:59
  • I don't understand what you mean "the path of a variable". Variables do not exist in the file system namespace so they do not have a path. If you mean "is there a way to print the absolute path of a file" then `print(pathlib.Path("filename").resolve())` does that; you can also use the legacy `os.path.abspath("filename")` function as the argument to `print()`. – tripleee Mar 25 '21 at 07:19
  • Ok I think I'm starting to understand the concept now. Thank you so much for taking the time to explain this to me! I guess I'm looking for the output of running the code to create another file which it looks like you created with the updated answer to the original question. So just to make sure I'm understanding this pickle.dump(names, picklefile) will create a file in the .pkl format? Then if I wanted it in another format I would find a way to convert it to .csv or .ged for example? Sorry if this is really basic stuff. – Justin Benfit Mar 26 '21 at 02:51
  • I don't think ".pkl format" is strictly well-defined; but yes, that's a conventional extension for the format which Python's Pickle module writes. The Pickle format saves a serialized form of Python's internal state, which is quite distinct from the static data you can put in a pure text file like a CSV file (a Pickle can contain things like an iterator which is halfway through iterating through something). But of course, the data you are saving here is simple enough that it could easily be written to a CSV file instead; in some sense, saving only the essential data would be much better. – tripleee Mar 26 '21 at 05:46
  • 1
    I'm not familiar with .ged format but it looks like e.g. https://pypi.org/project/gedcompy/ should easily allow you to write that format from Python. – tripleee Mar 26 '21 at 05:46