By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,256 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,256 IT Pros & Developers. It's quick & easy.

how to find the drive in python/cygwin?

P: n/a
Hello:

I'm running python under cygwin and need to find the drive letter.
Cygwin has a way of calling my drives by a name relative to the Cygwin
directory, so I get things like /home/user rather than
/cygdrive/g/cygwin/home/usr, etc. How can I find the letter of the
drive, or in the above example, the letter 'g'?

My program needs to run on an external media that comes with Cygwin on
it. I have no control over what drive is assigned to that media, but
for some reason, I do need to know the letter.

Please help!

Thanks,

Mayer

Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 26 Apr 2005 11:29:26 -0700, Mayer <gm****@cs.bgu.ac.il> wrote:
Hello:

I'm running python under cygwin and need to find the drive letter.
Cygwin has a way of calling my drives by a name relative to the Cygwin
directory, so I get things like /home/user rather than
/cygdrive/g/cygwin/home/usr, etc. How can I find the letter of the
drive, or in the above example, the letter 'g'?
Generally, drive N gets mapped to /cygdrive/n/ but you could call the
'mount' command and parse the output to see what drive letter got
assigned to a given path. On my system, mount returns:

$ mount
C:\cygwin\usr\X11R6\lib\X11\fonts on /usr/X11R6/lib/X11/fonts type
system (binmode)
C:\cygwin\bin on /usr/bin type system (binmode)
C:\cygwin\lib on /usr/lib type system (binmode)
C:\cygwin on / type system (binmode)
c: on /cygdrive/c type user (binmode,noumount)
d: on /cygdrive/d type user (binmode,noumount)
g: on /cygdrive/g type user (binmode,noumount)
x: on /cygdrive/x type user (binmode,noumount)
z: on /cygdrive/z type user (binmode,noumount)
My program needs to run on an external media that comes with Cygwin on
it. I have no control over what drive is assigned to that media, but
for some reason, I do need to know the letter.


Why?

--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Jul 19 '05 #2

P: n/a

How about doing "cygpath -w /home/user" and picking the drive letter?

Raghu.

Jul 19 '05 #3

P: n/a
Forwarding to list, as you forgot to Reply-all (Don't worry, we all do
it at least once!).

---------- Forwarded message ----------
From: Mayer Goldberg <gm****@little-lisper.org>
Date: Apr 26, 2005 3:01 PM
Subject: Re: how to find the drive in python/cygwin?
To: Kristian Zoerhoff <kr***************@gmail.com>
Dear Kristian:

Thanks for the very fast reply! I wasn't thinking of the mount command
-- this is really the correct way to think about my problem.
My program needs to run on an external media that comes with Cygwin on
it. I have no control over what drive is assigned to that media, but
for some reason, I do need to know the letter.


Why?


Basically this has to do with moving around: I use different computers
throughout the day, and need to carry with me a sane and productive
computing environment. Rebooting to linux is not an option for me, so I
need a Windows solution. I found it in the form of a 40G pocket hard
drive with a USB connection. I plug in the connection, go to my software
subdirectory and run what I need. I have a mixture of Unix and Windows
programs, and often I have to run a Windows program instead of a unix
program (for example, I run miktex instead of the tetex that comes with
cygwin). The Windows utilities need to be passed arguments in DOS
format, i.e., something like G:\FOO\foo.dll. Cygwin is only one specific
application I use, so I didn't install everything under the / (which
would have solved many problems for me!). As a result, I need to know
the drive letter.

Thanks again,

Mayer
--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Jul 19 '05 #4

P: n/a
Hi All--
Use win32api to find drives:

====cut here====
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os.path
import win32api
import sys
def findAllDrives():
Drives=[]
print "Searching for drives..."
drives=win32api.GetLogicalDriveStrings().split(":" )
for i in drives:
dr=i[-1].lower()
if dr.isalpha():
dr+=":\\"
inf=None
try:
inf=win32api.GetVolumeInformation(dr)
except:
pass # Removable drive, not ready
# You'll still get the drive letter, but inf will be None
Drives.append([dr,inf])
return Drives

if __name__=="__main__":
drives=findAllDrives()
for i in drives:
print i[0],i[1]

====cut here====

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #5

P: n/a
Ivan,

On Tue, Apr 26, 2005 at 07:02:48PM -0600, Ivan Van Laningham wrote:
Use win32api to find drives:

====cut here====
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os.path
import win32api
[snip]


AFAICT, the win32api module has not been ported to Cygwin Python.

Jason

--
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6
Jul 19 '05 #6

P: n/a
Hi All--

Jason Tishler wrote:

Ivan,

On Tue, Apr 26, 2005 at 07:02:48PM -0600, Ivan Van Laningham wrote:
Use win32api to find drives:

====cut here====
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os.path
import win32api
[snip]


AFAICT, the win32api module has not been ported to Cygwin Python.


I'm not running Cygwin, but Uwin. I installed regular Python:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
^Z

with the win32api that matched. I have no trouble running it. Is there
some reason to prefer a Python compiled by the Cygwin tools?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #7

P: n/a
Ivan,

On Sat, Apr 30, 2005 at 07:29:32AM -0600, Ivan Van Laningham wrote:
Jason Tishler wrote:
On Tue, Apr 26, 2005 at 07:02:48PM -0600, Ivan Van Laningham wrote:
Use win32api to find drives:

====cut here====
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os.path
import win32api
[snip]


AFAICT, the win32api module has not been ported to Cygwin Python.


I'm not running Cygwin, but Uwin. I installed regular Python:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
^Z


with the win32api that matched. I have no trouble running it. Is
there some reason to prefer a Python compiled by the Cygwin tools?


It depends on your needs. If you are looking for a more Unix-like
Python, then the Cygwin version would probably be better. If
Windows-like, then the native Windows version would probably be better.

The OP seem to be interested in a Cygwin Python solution -- not a
Windows one. So, I was just clarifying that the win32api module is not
supported under Cygwin Python.

Jason

--
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6
Jul 19 '05 #8

P: n/a
Hi All--

Jason Tishler wrote:

Ivan,

It depends on your needs. If you are looking for a more Unix-like
Python, then the Cygwin version would probably be better. If
Windows-like, then the native Windows version would probably be better.

The OP seem to be interested in a Cygwin Python solution -- not a
Windows one. So, I was just clarifying that the win32api module is not
supported under Cygwin Python.


Could you clarify? I always thought that the only thing really
different were the default path assumptions--/ instead of \, and so
on--rather than anything substantive. I try to use os.path.sep() and
os.path.join(), etc.

What else could bite me? ;-)

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #9

P: n/a
Ivan,

On Sat, Apr 30, 2005 at 08:44:55AM -0600, Ivan Van Laningham wrote:
Jason Tishler wrote:
I was just clarifying that the win32api module is not supported
under Cygwin Python.


Could you clarify? I always thought that the only thing really
different were the default path assumptions--/ instead of \, and so
on--rather than anything substantive. I try to use os.path.sep() and
os.path.join(), etc.

What else could bite me? ;-)


Not much -- at least not too hard. :,) Anyway, only the low level stuff
would be different: Posix versus Win32, shared extensions, etc. The
high level stuff should be the same -- isn't Python just Python. :,)

Jason

--
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.