2

I am using arcpy to describe datasets and output the results into a csv file. I am doing this via a script in arctoolbox and would like to save the file in the same folder as the file being analysed. I don ́t want to overwrite any existing files, and thus would like to simply add a number to the file name.

In the FieldCalculator we can use %i% or %n% to have a variable suffix - for example if my output txt file is called output.txt and a second output.txt is created, this is automatically changed to output01.txt

I want to do the same with ModelBuilder but if I can ́t use %i%.

import arcpy, os
from arcpy import env
import os.path
env.overwriteOutput = True
#Enter FeatureClass path in modelbuilder dialog
fc = arcpy.GetParameterAsText(0)
ischecked = arcpy.GetParameterAsText(1)
#Get FeatureClass name
fcname = os.path.basename(fc)
#define Output Directory
outputTxtFilePath = "PATH"
#Define filename
outputTxtFilename = "\\"+fcname+ %i% +".txt"
#full path of file
outputTxtFile = outputTxtFilePath+outputTxtFilename
# Create a list of fields using the ListFields function
fields = arcpy.ListFields(fc)
#open csv file
csv = open(outputTxtFile,'w')
#write describe results to txt file
csv.write(fc)
for field in fields:
 #create string
 txtStr = "Feldname: {0} \n Type: {1} \n Länge {2}".format(field.name, field.type, field.length) + '\n' 
 csv.write(txtStr)
csv.close()
#Open the txt file if is checked
if str(ischecked) == 'true':
 os.system('notepad '+outputTxtFile)
else:
 exit

it works ok if I remove %i%, but then each file gets overwritten.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 16, 2015 at 8:59
7
  • is %i% meant to represent the date or number? Commented Jun 16, 2015 at 9:12
  • Would you be able to edit your question to include a code snippet that works up to the point where you are stuck, please? For any ArcPy questions I think providing a code snippet is your best starting point to attract potential answerers here. Commented Jun 16, 2015 at 9:21
  • yes...ArcGIS can use %i% for an integer or %n% for an existing name. Commented Jun 16, 2015 at 9:21
  • Using %i% for an integer or %n% for an existing name seems to be a ModelBuilder thing and unrelated or only loosely related to ArcPy and GetParameterAsText. Can you include a code snippet to show how you are using it here, please? Commented Jun 16, 2015 at 9:45
  • doh, I just finished some code for this (ill save it for now), but like @PolyGeo said, code snippet would be good just to see how its put together Commented Jun 16, 2015 at 9:50

1 Answer 1

2

You could add a python function that pulls any number value from the defined output text file name and adds or assigns 1 to it:

def getValue(x): 
 try:
 return ''.join(i for i in x if i.isdigit()) 
 except ValueError:
 pass
fcname = os.path.basename(fc)
fcnameNoEXT = fcname.split('.')[0]
i = 1
if os.path.exists(fc):
 value = getValue(fcname)
 if value:
 newValue = i + int(value)
 outputTxtFilename = "\\"+fcnameNoEXT.replace(str(value),str(newValue))+ ".txt"
 else:
 outputTxtFilename = "\\"+fcnameNoEXT+ "{}.txt".format(i)
answered Jun 16, 2015 at 11:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.