Interpreting "freeform" file paths?

205
5
Jump to solution
06-29-2018 11:02 AM
KeithD1
New Contributor III

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:

  1. How to pass the location argument if it include spaces. The below approach does not work in this scenario. Shall I enclose the arguments in some delimiter? If so, how do I parse the arguments then? Note that there are other arguments as well.
  2. How to split apart outFile into the path and the filename. These will be used by arcpy.TableToTable_conversion(), which requires these as separate parameters.
  3. How to reformat the path such that it includes proper escaping such as the double-backlashes.

# 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)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

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'))

View solution in original post

5 Replies
RandyBurton
MVP Regular Contributor

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.

KeithD1
New Contributor III

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.

0 Kudos
RandyBurton
MVP Regular Contributor

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.

DarrenWiens2
MVP Honored Contributor

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'))

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

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.