Re: RE Despair - help required
Thank you all guys. It seems like the simpler the solution, the more I am happy about it. Sorry, for the simple question, I am quite new to this lang. Cheers. Robert Kern wrote: > Yoav wrote: > >>Don't think it will do much good. I need to get them from a file and >>extract the last folder in the path. For example: >>if I get "c:\dos\util" >>I want to extract the string "\util" > > > You mean like this: > > import os > os.path.sep + os.path.split(r"c:\dos\util")[-1] > -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
In article <[EMAIL PROTECTED]>, Yoav <[EMAIL PROTECTED]> wrote: >Fredrik Lundh wrote: >> "Yoav" wrote: >>>I am trying the following: >>> >>>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") >> instead of struggling with weird REs, why not use Python's standard >> filename manipulation library instead? >> >> http://docs.python.org/lib/module-os.path.html >Don't think it will do much good. I need to get them from a file and >extract the last folder in the path. For example: >if I get "c:\dos\util" >I want to extract the string "\util" Did you actually look at the docs Fredrik pointed you at? Did you, in particular, notice os.path.basename, which does (almost) exactly what you want? -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | "Frankly I have no feelings towards penguins one way or the other" \X/ |-- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav wrote: > Don't think it will do much good. I need to get them from a file and > extract the last folder in the path. For example: > if I get "c:\dos\util" > I want to extract the string "\util" You mean like this: import os os.path.sep + os.path.split(r"c:\dos\util")[-1] -- Robert Kern [EMAIL PROTECTED] "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list
RE: RE Despair - help required
Hi, > Don't think it will do much good. I need to get them from a file and > extract the last folder in the path. For example: > if I get "c:\dos\util" > I want to extract the string "\util" Still, os.path is your friend: import os filepath = r'C:\dos\util' base, last = os.path.split(filepath) print base # 'C:\dos' print last # 'util' print os.sep+last # '\util' Don't forget to read > > http://docs.python.org/lib/module-os.path.html for some more info! Regards, Mc! -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav wrote: > Don't think it will do much good. I need to get them from a file and > extract the last folder in the path. For example: > if I get "c:\dos\util" > I want to extract the string "\util" Then os.path.basename should be for you. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav wrote: > Don't think it will do much good. I need to get them from a file and > extract the last folder in the path. For example: > if I get "c:\dos\util" > I want to extract the string "\util" like frederik says (I use '/' as I am using Unix): >>> import os >>> os.path.split ('c:/foo/bar') ('c:/foo', 'bar') >>> os.path.splitext ('c:/foo/bar') ('c:/foo/bar', '') >>> os.path.splitext ('c:/foo/bar.txt') ('c:/foo/bar', '.txt') or if you are really reluctant: >>> 'c:\\foo\\bar'.split ('\\') ['c:', 'foo', 'bar'] >>> 'c:\\foo\\bar'.split ('\\') [-1] 'bar' > Fredrik Lundh wrote: >> instead of struggling with weird REs, why not use Python's standard >> filename manipulation library instead? >> >> http://docs.python.org/lib/module-os.path.html >> >> >> >> -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Don't think it will do much good. I need to get them from a file and extract the last folder in the path. For example: if I get "c:\dos\util" I want to extract the string "\util" Fredrik Lundh wrote: > "Yoav" wrote: > >>I am trying the following: >> >>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") >> >>and I get a return of NoneType, and I have no idea why. I know that I >>missing something here, but I really can't figure out why > > > instead of struggling with weird REs, why not use Python's standard > filename manipulation library instead? > > http://docs.python.org/lib/module-os.path.html > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Thanks guys. Issue solved. I am also going to give Microsoft a call about it. Any other issues you want me to raise while I am talking to them? Cheers. Robert Kern wrote: > Yoav wrote: > >>I am trying the following: >> >>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") >> >>and I get a return of NoneType, and I have no idea why. I know that I >>missing something here, but I really can't figure out why (I bet it's >>something obvious). I also tried this RE on KODOS and it works fine >>there, so I am really puzzled. >> >>Any ideas? > > > Look at the second string. It has "\r" in the middle of it where you > really want "\\r" (or alternatively r"c:\ret_files"). > -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
"Yoav" wrote: >I am trying the following: > > re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") > > and I get a return of NoneType, and I have no idea why. I know that I > missing something here, but I really can't figure out why instead of struggling with weird REs, why not use Python's standard filename manipulation library instead? http://docs.python.org/lib/module-os.path.html -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav enlightened us with: > I am trying the following: > > re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") > > and I get a return of NoneType, and I have no idea why. Because you don't match a carriage return "\r". > I know that I missing something here, but I really can't figure out > why (I bet it's something obvious). Use forward slashes instead of backward slashes. And go nag at Microsoft for using the most widely used escape character as a path separator... Sybren -- The problem with the world is stupidity. Not saying there should be a capital punishment for stupidity, but why don't we just take the safety labels off of everything and let the problem solve itself? Frank Zappa -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav wrote: > I am trying the following: > > re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files") > > and I get a return of NoneType, and I have no idea why. I know that I > missing something here, but I really can't figure out why (I bet it's > something obvious). I also tried this RE on KODOS and it works fine > there, so I am really puzzled. > > Any ideas? Look at the second string. It has "\r" in the middle of it where you really want "\\r" (or alternatively r"c:\ret_files"). -- Robert Kern [EMAIL PROTECTED] "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list