(ANSWERED - THANKS) Using str(number) in a file path

This is the place for queries that don't fit in any of the other categories.

(ANSWERED - THANKS) Using str(number) in a file path

Postby kadinlucas » Sun Apr 14, 2013 10:51 pm

Hi, I have just started using python to make some of my ArcGIS tasks run quicker.

I have some code that effectively takes a line, splits it into points, attaches those points to another set of points that move back in time, so that I can see where the line would be back in time (It is for paleogeography/geology research).

I know the code itself works as I am currently running it by manually editing the 'Age' field and age in the Lines variable, the code then runs for a minute and I do it again:

Lines = r'R:\Paleogeography Maps\Northland\shapefiles\2Ma.shp"'
Age = 2
#then the rest of the code follows#

What I am trying to do is automate this so I can set a start and end point and let it run through it all for me.
The problem I am having is with converting the Age number into a string in the file path, I keep getting error: does not exist or is not supported

The code I tried is:

count = 2
... while count < 28:
... Lines = r'R:\Paleogeography Maps\Northland\shapefiles\'str(count)'+"Ma.shp"'
... Age = count
#and the rest of the code, then at the bottom count = count +1

Any idea how I write the str(count)+Ma.shp part. I just can't get the syntax correct so that it uses the correct file.

Thanks in advance,

Kadin
Last edited by kadinlucas on Mon Apr 15, 2013 12:06 am, edited 1 time in total.
kadinlucas
 
Posts: 4
Joined: Sun Apr 14, 2013 10:43 pm

Re: Using str(number) in a file path (code help please)

Postby Yoriz » Sun Apr 14, 2013 11:20 pm

Hi
Please read this post on how to post questions/code ect
Code: Select all
Lines = r'R:\Paleogeography Maps\Northland\shapefiles\{}Ma.shp'.format(count)

Here's a link to string formating
http://docs.python.org/3/library/string.html#format-string-syntax
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Using str(number) in a file path (code help please)

Postby kadinlucas » Sun Apr 14, 2013 11:26 pm

Code: Select all
count = 5
...
... while count < 28:
...
...     Lines = r'R:\Paleogeography Maps\Northland\shapefiles\"str(count)"+"Ma.shp"'
...
...     Age = count
...
...     DB = r'R:\PMap_Master\PMap_Database_UOW\NZ_Paleo_Geography.gdb'
...
...     Wksp = r'R:\Paleogeography Maps\Northland\Paleobathymetry_backintime'
...
...     arcpy.env.workspace = Wksp


Just can't figure out the correct way to write the "str(count)"+"Ma-shp" part of the file path so that it just finds the file called 5Ma.shp in this example.
kadinlucas
 
Posts: 4
Joined: Sun Apr 14, 2013 10:43 pm

Re: Using str(number) in a file path (code help please)

Postby Yoriz » Sun Apr 14, 2013 11:29 pm

Thanks for posting your question with code tags, the answer is in the code tags in my post above, plus a link to where in the python docs it is explained.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Using str(number) in a file path (code help please)

Postby kadinlucas » Sun Apr 14, 2013 11:40 pm

Sorry I didn't see that the code you posted was actually different, I thought you were giving me an example of how to post it...
Thank you for the effort in trying to help, unfortunately most of that article is over my head. I received the following message so am currently trying to figure it out
Code: Select all
Runtime error <type 'exceptions.ValueError'>: zero length field name in format

Only thing I have found so far is that it may just be different syntax for different versions of python, I have 2.6.5.
kadinlucas
 
Posts: 4
Joined: Sun Apr 14, 2013 10:43 pm

Re: Using str(number) in a file path (code help please)

Postby Yoriz » Sun Apr 14, 2013 11:50 pm

try this bit of code
Code: Select all
count = 10
Lines = r'R:\Paleogeography Maps\Northland\shapefiles\{}Ma.shp'.format(count)
print Lines

it prints
Code: Select all
R:\Paleogeography Maps\Northland\shapefiles\10Ma.shp


it does this by replacing the {} with count

You might need to change the print as im on python version 2
If your using python 3 change the print to
Code: Select all
print(Lines)

I think the error your getting is from another part of your code
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Using str(number) in a file path (code help please)

Postby kadinlucas » Sun Apr 14, 2013 11:58 pm

Hi Yoriz, something I read said that : Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.
From this I changed your original code to have a 0
Code: Select all
Lines = r'R:\Paleogeography Maps\Northland\shapefiles\{0}Ma.shp'.format(count)

I have no idea what that actually does to the code but it is now running through perfectly!

Thank you very much sir,

Kadin
kadinlucas
 
Posts: 4
Joined: Sun Apr 14, 2013 10:43 pm

Re: Using str(number) in a file path (code help please)

Postby Yoriz » Mon Apr 15, 2013 12:00 am

Your welcome

There is a tutorial on this forum on python strings that has a part on formatting that might be easier for you to follow
http://www.python-forum.org/viewtopic.php?f=25&t=168

The 0 is the index position it takes the value from
view this code and its output
Code: Select all
print 'Some text: {0}, {0}, {1}'.format('TextPos 0', 'TextPos 1')

Code: Select all
Some text: TextPos 0, TextPos 0, TextPos 1

Notice that where ever there is {0} it uses 'TextPos 0' which is at index 0 of the values passed to format and where there is {1} it uses 'TextPos 1' which is at index 1 of the values passed to format
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: 7stud and 3 guests