analyzing removable media

G

glenn

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
 
J

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
 
G

glenn

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
 
J

John Machin

glenn said:
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.
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
N

Nick Vatamaniuc

glenn said:
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.
 
J

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.
 
G

glenn

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
 
G

glenn

Nick said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top