[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

how to resolve Windows pathnames into cygwin ones

mgierdal@gmail.com

1/18/2008 4:11:00 PM

I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
a Windows form and none of os.path.* functions seem to resolve it to a
cygwin form. Rather they _append_ it to the current directory,
resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
It's all being developed under cygwin currently (so it is a kind of
mixed environment), but I would like the fix to work correctly in any
environment.

Thanks,
Marcin
6 Answers

Gerhard Häring

1/18/2008 4:42:00 PM

0

mgierdal@gmail.com wrote:
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.

You can call the cygpath utility:

>>> import commands
>>> commands.getoutput("cygpath c:/windows")
'/c/windows'

-- Gerhard

apatheticagnostic

1/18/2008 4:44:00 PM

0

On Jan 18, 11:10 am, "mgier...@gmail.com" <mgier...@gmail.com> wrote:
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.
>
> Thanks,
> Marcin

Well, you could write it yourself....

(assuming path is a string, here's a first go at it...)
def path_into_cygpath(path):
drive, destination = path.split(':')
newpath = '/cygdrive/' + drive.lower() + destination
return newpath

Grant Edwards

1/18/2008 4:49:00 PM

0

On 2008-01-18, apatheticagnostic <apatheticagnostic@gmail.com> wrote:
> On Jan 18, 11:10 am, "mgier...@gmail.com" <mgier...@gmail.com> wrote:
>> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
>> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
>> a Windows form and none of os.path.* functions seem to resolve it to a
>> cygwin form. Rather they _append_ it to the current directory,
>> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
>> It's all being developed under cygwin currently (so it is a kind of
>> mixed environment), but I would like the fix to work correctly in any
>> environment.
>>
>> Thanks,
>> Marcin
>
> Well, you could write it yourself....
>
> (assuming path is a string, here's a first go at it...)
> def path_into_cygpath(path):
> drive, destination = path.split(':')
> newpath = '/cygdrive/' + drive.lower() + destination
> return newpath

Don't forget to convert backslashes into forward slashes.

--
Grant Edwards grante Yow! TONY RANDALL! Is YOUR
at life a PATIO of FUN??
visi.com

apatheticagnostic

1/18/2008 5:13:00 PM

0

On Jan 18, 11:48 am, Grant Edwards <gra...@visi.com> wrote:
> On 2008-01-18, apatheticagnostic <apatheticagnos...@gmail.com> wrote:
>
>
>
> > On Jan 18, 11:10 am, "mgier...@gmail.com" <mgier...@gmail.com> wrote:
> >> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> >> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> >> a Windows form and none of os.path.* functions seem to resolve it to a
> >> cygwin form. Rather they _append_ it to the current directory,
> >> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> >> It's all being developed under cygwin currently (so it is a kind of
> >> mixed environment), but I would like the fix to work correctly in any
> >> environment.
>
> >> Thanks,
> >> Marcin
>
> > Well, you could write it yourself....
>
> > (assuming path is a string, here's a first go at it...)
> > def path_into_cygpath(path):
> > drive, destination = path.split(':')
> > newpath = '/cygdrive/' + drive.lower() + destination
> > return newpath
>
> Don't forget to convert backslashes into forward slashes.
>
> --
> Grant Edwards grante Yow! TONY RANDALL! Is YOUR
> at life a PATIO of FUN??
> visi.com

Whoops.

Here we go then (are forward slashes valid in a filename in windows?)
def path_into_cygpath(path):
drive, destination = path.replace('\\','/').split(':')
return '/cygdrive/' + drive.lower() + destination

Reedick, Andrew

1/18/2008 5:26:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of
mgierdal@gmail.com
> Sent: Friday, January 18, 2008 11:11 AM
> To: python-list@python.org
> Subject: how to resolve Windows pathnames into cygwin ones
>
> I am looking for a function to resolve 'F:/foo/bar' into '/cygdrive/f/
> foo/bar'. I get the original dirpath from tkFileDialog.askdirectory in
> a Windows form and none of os.path.* functions seem to resolve it to a
> cygwin form. Rather they _append_ it to the current directory,
> resulting at best in a monster '/cygdrive/c/whatever/f/foo/bar'.
> It's all being developed under cygwin currently (so it is a kind of
> mixed environment), but I would like the fix to work correctly in any
> environment.
>

Firstly, '/cygdrive' is overrated. Cygwin will accept: 'ls c:\\foo'
and 'ls c:/foo'


s= 'f:\\foo\\bar'

print re.sub(r'\\', '/', s)

m = re.match('^(.):(.*)$', s)
print '/cygdrive/' + m.group(1) + re.sub(r'\\', '/', m.group(2))

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621


mgierdal@gmail.com

1/18/2008 8:14:00 PM

0

Well yes, I was hoping for a library function, but none provides it.
Thanks for the code. Works nicely.

On Jan 18, 12:12 pm, apatheticagnostic <apatheticagnos...@gmail.com>
wrote:
> Here we go then (are forward slashes valid in a filename in windows?)
> def path_into_cygpath(path):
> drive, destination = path.replace('\\','/').split(':')
> return '/cygdrive/' + drive.lower() + destination