Determining the volume ID for a file/drive?

Bengt Richter bokr at oz.net
Tue Apr 29 19:56:11 CEST 2003


On Tue, 29 Apr 2003 13:45:03 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Alex Martelli wrote:
>   ...
>> Quick and dirty...:
>> 
>> import os
>> x = os.popen('C:\\')
>
>OOPS -- TOO quick -- sorry, I meant os.popen('DIR C:\\')!!!
>
Or
 >>> import os
 >>> os.popen('vol C:').readlines()
 [' Volume in drive C is System\n', ' Volume Serial Number is 14CF-C4B9\n']

 >>> def drive2volser(drive):
 ...     import os
 ...     return os.popen('vol %s'%drive).readlines()[1].split()[-1]
 ...
 >>> drive2volser('C:')
 '14CF-C4B9'

(Note that volume labels can have spaces, so you might want to .split(' is ')
and .strip() if you want that).

Regards,
Bengt Richter




More information about the Python-list mailing list