Append new data to end of text file

Anyone know how or if there is a way to have dynamo append to the end of a text file new data, instead of re-writing the entire file? Or any other file type like .csv, .json, .xml? This would be super useful and right now it doesn’t appear there is any logic for this. I’m especially interested in the ability for two or more different machines to write to the same file without overriding one another.

Thanks!

1 Like

@Josh_Moore
Yes there is, its actually very very simple, i assume (apologies for my assumption) that you are new to dynamo? You can use python in dynamo. Python is very flexible and can write to files either appending or create new. Python is what is used to create a pdf with customized filenaming. I do not know how it will behave for your purpose but to answer your question yes dynamo can write to file via python.

If others have other answers they will let you know.

See this to start you http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

Apologies again for assuming…just trying to help.

WriteIdsOfVisibleElementsToCSVFile.dyn (12.9 KB)

I got today a similar problem, and the node FileSystem.AppendText created by @Radu_Gidei on GitHub is not available for me in 1.3.2 - I don’t know why.

The top script on the image will append a list with strings to a csv file, seperated with commas. In my case these are IDs.

you can use this python code below:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#Die Eingaben für diesen Block werden in Form einer Liste in den IN-Variablen gespeichert.
filepath = IN[0]
input = IN[1]

with open(filepath, 'a') as file:
	for line in input:
		file.write(line)
		file.write(",")

#Weisen Sie Ihre Ausgabe der OUT-Variablen zu.
OUT = "success"

As mentioned by @Thomas_Vogt, there is a new AppendText node that i’ve added to Dynamo Core, but it seems it will only be available in Dynamo 2.0

In the meantime, the simplest way (but inefficient, performance depends on your text size greatly) is to read the contents of the file as a string using ReadFromFile node, append the text using Concat node then write the result back to a/same file.

You can also use Python as per above.

Edit : note this does not address the need for concurrent users.

1 Like

@Josh_Moore

I would suggest using a database that supports concurrent users, and will naturally append data.

It sounds complicated, but something like SQLite is super-simple.
Search for my name & SQLite on this forum, or if you have a specific query- provide more details/sample files

Andrew

You can use “\n” while append new data to file.

with open("index.txt", "a") as myfile:
    myfile.write("First Line\n")
    myfile.write("Second Line\n")