Skip to content

Instantly share code, notes, and snippets.

@StepMaher
Last active December 8, 2015 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StepMaher/fe76ee53d127548b69f8 to your computer and use it in GitHub Desktop.
Save StepMaher/fe76ee53d127548b69f8 to your computer and use it in GitHub Desktop.
"""
Writes to a TXT or CSV file.
Inputs:
relative: Set to 'True' to create a new Text File using relative path. This file will live in the same directory as the Grasshopper document.
file: TXT or CSV file to write to.
lines: Text to write.
write: Set to True to write.
overwrite: Set to True to overwrite the specified file.
append: Set to True to append the specified file.
"""
# Stephen P. Maher / NBBJ / 2015
ghenv.Component.Name = "Text Document Writer"
ghenv.Component.NickName = 'TXT-Writer'
import Grasshopper as gh
import os
if not relative and not file:
msg = "Set either 'relative' to True or connect a file path to input parameter 'file'."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
elif relative == True:
if overwrite == True and append == True:
msg = "Set either 'overwrite' or 'append' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)
elif write:
gh_path = ghdoc.Path # Get GH document's file path
name = gh_path.split(".")[0]
new = name.split("\\")
file_name = new[4] + "_text_file.txt" # Create text file name
dir = os.path.dirname(os.path.realpath(gh_path))
file = os.path.join(dir, file_name) # Create text file path
filePath = open(file, "w") # Open text file to create it
if os.stat(file).st_size == 0: # Check to see if file has any data and continue if it's empty
filePath = open(file, "w") # Open the file
for line in lines: # Iterate through lines
filePath.write(line + "\n") # Write separate lines
filePath.close() # Close the file
elif os.stat(file).st_size >= 1 and overwrite == False and append == False: # Check if file has data; if it does and 'overwrite' is set to False, warn the user
msg = "This file has content. If you wish to overwrite or append the file, please set either input parameters 'overwrite' or 'append' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)
elif os.stat(file).st_size >= 1 and overwrite == True: # If file has data and 'overwrite' set to True continue
filePath = open(file, "w") # Open the file
for line in lines: # Iterate through lines
filePath.write(line + "\n") # Write separate lines
filePath.close() # Close the file
elif os.stat(file).st_size >= 1 and append == True: # If file has data and 'append' set to True and 'overwrite set to False continue
filePath = open(file, "a") # Open the file and 'append' document
for line in lines: # Iterate through lines
filePath.write("\n" + line) # Write separate lines
filePath.close() # Close the file
else:
msg = "Set 'write' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
elif relative == False or not relative:
open(file, "w")
if file:
if overwrite == True and append == True:
msg = "Set either 'overwrite' or 'append' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)
elif write:
if os.stat(file).st_size == 0: # Check to see if file has any data and continue if it's empty
filePath = open(file, "w") # Open the file
for line in lines: # Iterate through lines
filePath.write(line + "\n") # Write separate lines
filePath.close() # Close the file
elif os.stat(file).st_size >= 1 and overwrite == False and append == False: # Check if file has data; if it does and 'overwrite' is set to False, warn the user
msg = "This file has content. If you wish to overwrite or append the file, please set either input parameters 'overwrite' or 'append' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Error, msg)
elif os.stat(file).st_size >= 1 and overwrite == True: # If file has data and 'overwrite' set to True continue
filePath = open(file, "w") # Open the file
for line in lines: # Iterate through lines
filePath.write(line + "\n") # Write separate lines
filePath.close() # Close the file
elif os.stat(file).st_size >= 1 and append == True: # If file has data and 'append' set to True and 'overwrite set to False continue
filePath = open(file, "a") # Open the file and 'append' document
for line in lines: # Iterate through lines
filePath.write("\n" + line) # Write separate lines
filePath.close() # Close the file
else:
msg = "Set 'write' to True."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
else:
msg = "Input parameter 'file' failed to collect data."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment