I am writing a standalone Python script which involves exporting a geodatabase table to a CSV at a location specified by the user. The script will be called via the command line, and the location is one of the arguments.
Ideally, the user would be able to specify the location in a way that's familiar to them. They are not Python coders, and the script is non-interactive so I can't present a dialog box where they select the location. Most likely, they will copy-paste a location from Windows Explorer.
There are three areas where I'm seeking assistance:
# retrieve outFile from the command line (2nd argument)
outFile = sys.argv[2] # represents a filename and path
# example: c:\temp\output file.csv
# could contain spaces, UNC paths, etc.
# split outFile into a pathway and a filename
outPath =
outFileName =
# export targetTable to the file specified in outFile
# note: targetTable is created elsewhere
arcpy.TableToTable_conversion(targetTable,outPath,outFileName)
Solved! Go to Solution.
You need to enclose parameters containing spaces with quotes to keep it together.
There are several ways to deal with paths using the os.path module. Bonus: it smartly handles slashes for you.
import os
path = 'some/path/some_file.txt'
print(os.path.dirname(path), os.path.basename(path), os.path.splitext(path))
('some/path', 'some_file.txt', ('some/path/some_file', '.txt'))
Looks like you are using a command line. Have you considered creating a Python script tool. It offers some parameter checking. See Understanding script tool parameters and related documentation pages.
Thanks Randy.
It looks like the "script tool" would enable the script to be used directly from Arc? Would that be at the expense of not being able to use it via CLI?
I'm calling the Python script directly from another application using its command line interface. There are a few functions for which custom-built ArcTools exist (developed in-house) and this is how I'm accessing them, rather than trying to recreate them in the other application's programming language. The end-goal is for the Python script to be seamless to the end-user...they will simply see a black window appear for a moment before control is returned to the other application. So...I want to avoid any situation where the script may "error out" and leave them in a confused state.
As I recall, you should be able to use a script tool either inside or outside of Arc. A shortcut to a python script could activate it. Perhaps ImportToolbox; see last code sample.
Edit:
Actually, the tool dialog doesn't show outside Arc. For a dialog box, TkFileDialog might provide a similar user experience when outside Arc for searching directories.
You need to enclose parameters containing spaces with quotes to keep it together.
There are several ways to deal with paths using the os.path module. Bonus: it smartly handles slashes for you.
import os
path = 'some/path/some_file.txt'
print(os.path.dirname(path), os.path.basename(path), os.path.splitext(path))
('some/path', 'some_file.txt', ('some/path/some_file', '.txt'))
When specifying a path as an argument on the Windows Command Prompt, paths with spaces need to be quoted. Such need/behavior is documented in various places on Microsoft websites. If someone uses Copy as path in Windows Explorer or file/folder name completion in the command prompt, the quotes are inserted for the user.
If you want to cover your bases and account for a broad range of users, including those for which a "command prompt" is advanced, I suggest using a file open dialog box. You state that the script is non-interactive, but then you also state that the user is inputting the path on the command line, which is a form of interaction. Since 24.1. Tkinter — Python interface to Tcl/Tk — Python 2.7.15 documentation is packaged with Python, including the Python distributions with ArcGIS, I recommend inserting something simple as suggested in openfiledialog - Quick and easy file dialog in Python? - Stack Overflow
import Tkinter, tkFileDialog
root = Tkinter.Tk()root.withdraw()
file_path = tkFileDialog.askopenfilename()
The above code will let a user select the file and all of the spaces will be addressed, so you don't have to worry about them.