Mike
Mike

Reputation: 4405

How to Test if file contains full path or just file name, Python?

I have a script that I run from a GUI. The GUI is run in ArcGIS and basically, the first parameter can accept a file already embedded in ArcMap or I can use a explorer dialogue to navigate to the file locations.

If I use the option to choose the embedded file, then it only returns the file name:

i.e. 'SOIL_LINES'

If I use the navigation option, then it returns the file name and it's fill path:

C:\SOIL\SOIL_LINES.shp

I would like to test the input parameter to see if the file is just the file name or if it also includes the full path. I eventually need to parse out the path in option 2 to isolate the file name itself. That part I have figured out already.

I've used os.path.isfile but that will return the file in both scenarios because they are both considered a file.

Thanks, Mike

Upvotes: 6

Views: 4480

Answers (3)

J Agustin Barrachina
J Agustin Barrachina

Reputation: 4090

The pathlib alternative would be to do:

$ filename = "my/path/file.py"
$ Path(filename).name == filename
False
$ filename = "file.py"
$ Path(filename).name == filename 
True

Upvotes: 2

Alison R.
Alison R.

Reputation: 4294

In addition to isabs() mentioned in another answer to test if it is a full path, you can use os.path.basename() and os.path.dirname() to find just the filename or just the directory path. No need to parse them out yourself.

There's lots of cool stuff in that library. I recommend reading the full documentation on it-- it's not long. http://docs.python.org/2/library/os.path.html

Upvotes: 5

mgilson
mgilson

Reputation: 309841

You could probably use os.path.isabs for this ...

Upvotes: 4

Related Questions