Writing to text file using python node

I am trying to export some formatted message from python node to a text file.

But I am not getting the output because of some error. I have pasted the screenshot of the code and the error in node.

Please help.!
Screenshot%20(6)|690x388

!

While simply writing to a file e.g. ‘text.txt’ is valid for a normal python script, you need to provide the full path for writing in dynamo.

import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import os

filedir = r'C:\test'
filename = 'text.txt'
filepath = os.path.join(filedir, filename)
message = 'example message’
with open(filepath, 'w') as f:
    f.write(message)
1 Like

Hi All

I am trying to append text to a .txt file using python, but I can’t seem to get it to work.
This is something I have done several times before outside the Dynamo environment.
I even imported the entire boilerplate to see if I was missing anything, but that did not help.
What could I be missing?
See the code with the full boilerplate below.
Thanks for your help.

# Import Libraries.
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

import os
import logging
from datetime import datetime


# Inputs.
script_name = IN[0]
file_path = IN[1]

# Set the working directory.
try:
    os.chdir(r'your path')
except:
    print("Server working directory not set.")
    
# Configure error logging file.
logging.basicConfig(filename='script_usage_log.log',\
    level = logging.ERROR)

# Get current user.
try:
    current_user = os.environ.get('USERNAME').title()
except Exception as e:
    logging.error(f'Current user login NOT gotten. {os.getlogin().title()} PC.'
        f' {datetime.now()}. Exception: {e}.')
    sys.exit()     

# Get the document path.
try:
    document_name = doc.PathName
except Exception as e:
    logging.error(f'Document Path NOT gotten. {os.getlogin().title()} PC.'
        f' {datetime.now()}. Exception: {e}.')
    sys.exit()   

# Check if the document is workshared. 
try:
    document_isworkshared = doc.IsWorkshared
    if document_isworkshared == True:
        workshare_status = "Workshared"
    else:
        workshare_status = "Not Workshared"
except Exception as e:
    logging.error(f'Document workshare status NOT gotten. {os.getlogin()} PC.'
        f' {datetime.now()}. Exception: {e}.')
    sys.exit()
    
# Get the current date and time.
try:
    time_now = datetime.now()
except Exception as e:
    logging.error(f'Current time NOT gotten. {os.getlogin()} PC.'
        f' {datetime.now()}. Exception: {e}.')
    sys.exit()
    
# Write information to file. 
file = open(file_path, 'a')
write_content = ("User=" + current_user + " | Script Name= " + script_name +
    " | Workshare Status= " + workshare_status + " | File Path= " + 
    file_path + " | Time= " + str(time_now))
file.write(write_content)

I figured it out, thanks.
I needed a file.close() after the file.write()
Thank you.