1

I'm starting to learn Python and batch files. I have a Python script which selects data by location and exports the selection to a new feature class. The script runs just fine - included in this script I have the following lines:

matchCount = int(arcpy.GetCount_management(featureLayer).getOutput(0))

print str(matchCount) + " rows exported"

When I run this script in PythonWin the print statement above is produced in the Interactive Window. When I run this script through a batch file the print statement isn't produced. At the end of my batch file I have written PAUSE so the screen doesn't disappear after running my script. Anyone have ideas on how to produce my print statement in command line?

4
  • 1
    you could invoke your script in the command prompt, and then it wont exit upon completion. You could click Start->Run, type "cmd" and hit enter. Then navigate to your script and run it. Commented Sep 15, 2011 at 16:45
  • Is it possible for you to invoke your python script from the command line without running the batch file? Commented Sep 15, 2011 at 18:26
  • Thank you, Allan. I was able to run my .bat file as you suggested but what I'm really after is getting the print statement in my Python script to print in the command line. I imagine that is possible? Within my Python script I want to print a count: matchCount = int(arcpy.GetCount_management(featureLayer).getOutput(0)) print str(matchCount) + " rows exported" When I run my batch file in command line the window states: Ran myscript.py Press any key to continue... I would like the print statement from my script, "X rows exported", to also print in the window as well. Thank you.
    – Patty Jula
    Commented Sep 15, 2011 at 18:33
  • 2
    I would suggest not using a batch file to invoke python scripts. Instead I would try invoking your python script directly from the command prompt. Any external functionality in the batch file can be coded in the Python script Commented Sep 15, 2011 at 18:40

2 Answers 2

0

Can you show us the contents of your batch file please?

(BTW, recommended practice is to use arcpy.AddMessage() rather than print, for portability between command line/batch files and ArcMap. But print is fine if you don't want your message to display inside ArcMap.)

3
  • Ah, thank you. I put an arcpy.AddMessage in my Python script and got that statement to print the matchCount when I ran the batch file. Below is my batch file if you were curious. Thanks again. @ECHO OFF REM Runs myScript.py C:\Python26\ArcGIS10.0\python.exe C:\project\python\myScript.py ECHO Ran myScript.py PAUSE
    – Patty Jula
    Commented Sep 16, 2011 at 14:52
  • Glad that helped, still weird that print doesn't work. Have you tried removing the @ECHO OFF line and calling your script with the -u argument: C:\Python26\ArcGIS10.0\python.exe -u C:\project\python\myScript.py Kind of stabs in the dark but I have similar batch scripts and it works for me... Commented Sep 19, 2011 at 11:14
  • OK, I removed the @ECHO OFF from my .bat file and inserted "-u" as you suggested. I also modified my script so the arcpy.AddMessage was commented out and inserted my print line. The batch file ran and my print statement displayed. Now I know two ways of printing statements in command line. Thank you.
    – Patty Jula
    Commented Sep 19, 2011 at 21:13
0

An easier way for you to do this, is to set up Eclipse to work with PyDev and you can run your code and debug it there. You actually debig it there, putting stops and watches in the code, to interogate the objects.

That's my advice.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.