always use os.path() to work with paths... why? because os.path works on all
os.

so:
p = r`c:\this\is\my\path\to\myFile.txt
os.path.dirname(p)  wil return: `c:\this\is\my\path\to`

and the same code will work on mac or linux
p = r`/this/is/my/path/to/myFile.txt`
os.path.dirname(p) will return `/this/is/my/path/to`

same this goes for os.path.join(), os.path.splitext(), etc, etc

Never use string splitting to change path, os.path is there for that ... and
you create a more robust code at the same time.

My 2 cents


On Fri, Mar 5, 2010 at 1:07 AM, Justin Rosen <[email protected]> wrote:

> 1.
>
> import os
>
> testPath = '/here/is/my/path/here/is/my/path/here/is/my/path'
> os.path.dirname(testPath)
>
> 2.
>
> from xml.etree.ElementTree import ElementTree
>
> xmlFile = '/path/to/xml/test.xml'
>
> tree = ElementTree()
> tree.parse(xmlFile)
> version = tree.getroot().get('myVersion')
>
> where /path/to/xml/test.xml
> <xmlTest test1="myPic" test2="1234" myVersion="6" />
>
> Obviously, your xml file is probably a bit more complex, so your use of
> ElementTree maybe different
> http://docs.python.org/library/xml.etree.elementtree.html
>
>
> On Mar 4, 2010, at 8:55 PM, Adam Mechtley wrote:
>
> I'm sure there are plenty of other ways, but I would do:
>
> 1.
> testPath[0:testPath.rindex('/')] or testPath[0:testPath.rindex('/')+1] if
> you really want that trailing slash for some reason
>
> 2.
> import re
> xml = '<xmlTest test1="myPic" test2="1234" myVersion="6">'
> tag = 'myVersion'
> value = re.split('\W+', xml[xml.index(tag)+len(tag):len(xml)])[1]
>
> On Thu, Mar 4, 2010 at 10:27 PM, efecto <[email protected]> wrote:
>
>> 1.
>> testPath = '/here/is/my/path/here/is/my/path/here/is/my/path'
>> I want to get this path from the variable above.
>> /here/is/my/path/here/is/my/path/here/is/my/
>> I could use testPath.split('/') then manually create the path but i
>> think they may be an easier way to generate the string.
>>
>> 2.
>> I'm reading in an xml file and would like to get the version '6' from
>> the string below.
>> How would you do that efficiently? Again I may use split function but
>> i'm sure there are more convenient ways
>> <xmlTest test1="myPic" test2="1234" myVersion="6">
>>
>> Thanks for reading.
>>
>> --
>> http://groups.google.com/group/python_inside_maya
>
>
>
> --
> http://groups.google.com/group/python_inside_maya
>
>
>  --
> http://groups.google.com/group/python_inside_maya
>

-- 
http://groups.google.com/group/python_inside_maya

Reply via email to