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

analyzing removable media

P: n/a
Hi
can anyone tell me how given a directory or file path, I can
pythonically tell if that item is on 'removable media', or sometype of
vfs, the label of the media (or volume) and perhaps any other details
about the media itself?
thanks
Glenn

Sep 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jay
Well, in linux you can get a lot of info about where a file is based
upon where it lies in the file system. For example, if the folder the
file is located in resides in the /media or /mnt directories, then the
file is, barring a few rare circumstances, located upon a removable
medium of some sort. Once you get the name of that directory that is
right below /media or /mnt, then you can cross check the name in
/etc/fstab file.

However, the contents of your post lead me to believe that you're
working in a win32 environment. That's a bit trickier. Things aren't
so simple. You could probably grab similar information from the full
path itself. For example, just getting the drive letter that the file
resides on could tell you a lot. Now, where you'd cross check that is
more of a mystery to me. I'd guess the registry? For an easier way of
accessing the registry, I believe that the wrapper pywin32 may be of
interest to you. It shouldn't be that hard considering that you'd only
be getting some information, not setting anything.

If by chance we're talking about MacOS, I'm of almost no help. In the
case of MacOS X, it has a unix core, so I'd imagine that the method I
described for linux could probably be adapted.

Jay

glenn wrote:
Hi
can anyone tell me how given a directory or file path, I can
pythonically tell if that item is on 'removable media', or sometype of
vfs, the label of the media (or volume) and perhaps any other details
about the media itself?
thanks
Glenn
Sep 29 '06 #2

P: n/a
Hi Jay
pls excuse top post - Im actually doing this project in linux, but am
wanting it to be cross platform. I definitley have to cater for win32
also. I was hoping that burried in sys or os or that there'd be some x
platform module that did all that stuff for me

thnks for reply though
glenn
Jay wrote:
Well, in linux you can get a lot of info about where a file is based
upon where it lies in the file system. For example, if the folder the
file is located in resides in the /media or /mnt directories, then the
file is, barring a few rare circumstances, located upon a removable
medium of some sort. Once you get the name of that directory that is
right below /media or /mnt, then you can cross check the name in
/etc/fstab file.

However, the contents of your post lead me to believe that you're
working in a win32 environment. That's a bit trickier. Things aren't
so simple. You could probably grab similar information from the full
path itself. For example, just getting the drive letter that the file
resides on could tell you a lot. Now, where you'd cross check that is
more of a mystery to me. I'd guess the registry? For an easier way of
accessing the registry, I believe that the wrapper pywin32 may be of
interest to you. It shouldn't be that hard considering that you'd only
be getting some information, not setting anything.

If by chance we're talking about MacOS, I'm of almost no help. In the
case of MacOS X, it has a unix core, so I'd imagine that the method I
described for linux could probably be adapted.

Jay

glenn wrote:
Hi
can anyone tell me how given a directory or file path, I can
pythonically tell if that item is on 'removable media', or sometype of
vfs, the label of the media (or volume) and perhaps any other details
about the media itself?
thanks
Glenn
Sep 29 '06 #3

P: n/a

glenn wrote:
Hi Jay
pls excuse top post - Im actually doing this project in linux, but am
wanting it to be cross platform. I definitley have to cater for win32
also. I was hoping that burried in sys or os or that there'd be some x
platform module that did all that stuff for me
Things don't get buried. If it's not in the documentation, it's not
there.

Sep 29 '06 #4

P: n/a
glenn wrote:
Hi
can anyone tell me how given a directory or file path, I can
pythonically tell if that item is on 'removable media', or sometype of
vfs, the label of the media (or volume) and perhaps any other details
about the media itself?
thanks
Glenn
It won't be trivial because one of the goals of the operating systems
is to hide the differences between various types of storage devices and
make them all look the same (i.e. a jump drive, a CD-ROM, a network
volume are presented uniformly to the user as just another path in the
file system).

But you can probably use guesswork on unix OS's, as somebody above
suggested, and examine the path. If the path contains common media
mount points then that should give you a clue. On Windows you cannot
easily know. For example if your file is on D:\file.txt, it is not
immediatly obvious if D is a jumpdrive, a networked drive, a hard
drive, or a CD-ROM.

The only thing I can think of is to try to examine the Windows registry
with the _winreg module. Here are the docs:
http://docs.python.org/lib/module--winreg.html There might be a key
burried in there some place that will tell you which drives are what
type.

Sep 29 '06 #5

P: n/a
Jay
Dennis is absolutely right. The reason you won't find a method in the
standard libraries for doing this task specifically is because python
aims to by cross-platform. If it were to support a method that worked
in the way you describe specifically for Windows, that would
essentially break python's cross-platform compatibility. It would
create a method that simply wouldn't work on two of the three major
operating systems. Sorry.

You could write a method or series of methods that does the task you're
describing and have the first method in the chain detect what operating
system it is running on (which I believe is possible through the
standard library) and route the request to the appropriate OS-specific
method.

Just a thought.

Jay
Dennis Lee Bieber wrote:
On 28 Sep 2006 23:23:23 -0700, "glenn" <gl***@tangelosoftware.net>
declaimed the following in comp.lang.python:
Hi Jay
pls excuse top post - Im actually doing this project in linux, but am
wanting it to be cross platform. I definitley have to cater for win32
also. I was hoping that burried in sys or os or that there'd be some x
platform module that did all that stuff for me

As soon as you ask for "cross platform" and something that is
device/OS specific... you have conflict.

From the win32 extensions:

win32file.GetDriveType
int = GetDriveType()

Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk,
or network drive.

Return Value
The result is one of the DRIVE_* constants.

I suspect Linux would require checking parameters in the fstab file.
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Sep 29 '06 #6

P: n/a
>
As soon as you ask for "cross platform" and something that is
device/OS specific... you have conflict.

From the win32 extensions:

win32file.GetDriveType
int = GetDriveType()

Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk,
or network drive.

Return Value
The result is one of the DRIVE_* constants.

I suspect Linux would require checking parameters in the fstab file.
Thanks for the tip Dennis

Sep 30 '06 #7

P: n/a

Nick Vatamaniuc wrote:
glenn wrote:
Hi
can anyone tell me how given a directory or file path, I can
pythonically tell if that item is on 'removable media', or sometype of
vfs, the label of the media (or volume) and perhaps any other details
about the media itself?
thanks
Glenn

It won't be trivial because one of the goals of the operating systems
is to hide the differences between various types of storage devices and
make them all look the same (i.e. a jump drive, a CD-ROM, a network
volume are presented uniformly to the user as just another path in the
file system).

But you can probably use guesswork on unix OS's, as somebody above
suggested, and examine the path. If the path contains common media
mount points then that should give you a clue. On Windows you cannot
easily know. For example if your file is on D:\file.txt, it is not
immediatly obvious if D is a jumpdrive, a networked drive, a hard
drive, or a CD-ROM.

The only thing I can think of is to try to examine the Windows registry
with the _winreg module. Here are the docs:
http://docs.python.org/lib/module--winreg.html There might be a key
burried in there some place that will tell you which drives are what
type.
ok - thanks - will look at that - Any one know what a Mac approach
might be? can I expect that playing anything I write with fstab or
`mount` will work on OSX?, or anyother BSD for that matter?

Sep 30 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.