[Tutor] how to check a user input path for dirve existence
Brian van den Broek
bvande at po-box.mcgill.ca
Mon Apr 5 22:11:28 EDT 2004
thanks for the response. I have some reactions interspersed below.
Alan Gauld said unto the world upon 05/04/2004 19:46:
>>check to an attempt to create the directory, I won't have ruled
>>out trying to create, say, J:\spam on a system without a J: drive.
> This presupposes that drive letters can be part of a path, which
> kind of limits things to CP/M(do we get Python for CP/M?) and
> Windows/DOS. (I think they are the only OS's that use drive
> letters as part of a path? Macs use drives but they have
> names and no other distinguishing features...)
Yes to the presupposition, but a qualified no to the limit, I
think. (Perhaps my intent was ill-described.) What I want is to
rule out non-existent drives being referenced in the user input
path. Since I work mostly with Win (never have managed to install
Linux on my laptop :-( ) that is most relevant to me. But what I
was aiming at was to disallow any path which begins X: where X is
some string and X: is not a valid drive letter. I might be wrong,
but for any OS not using L: (L a letter) as a drive name, the test
will still work, albeit trivially since no such string is ever a
valid drive name. But you are right, it won't catch non-existent
drive references on a box with a different OS.
> Given that limitation we know that the drive info can only consist
> of a single letter and a colon. So the only thing to test for is:
> if path == ':'
That is the limitation on valid drivenames. But I also want to
rule out user input like:
which attempts to use 'users:' as a drive name.
So, I don't think
if path == ':':
do some stuff
will work. That only gets it if the input respects the Windows/DOS
'drive names have a single letter' convention.
Maybe I am over-fretting about possible input errors, but I am
quite new to programming and recently read a text on general style
which suggested trying to 'user-error proof' your code as much as
Thinking about it more, though, I believe I've answered the
question of which method (the one here discussed or try/except
clauses) is better. Going this way will have to build a block of
code for testing for valid drive letters, one for testing that no
disallowed characters ('*', '?', etc.) are included and so forth.
Seems exception handling will be the way to go.
> Also my brain is still adjusting to Python & Programming after
> a weeks break so maybe I'm missing some complexity somewhere
> - like network drives maybe...
Break? I've heard of them, but thought they were a myth ;-)
> Alan G.
More information about the Tutor