[Tutor] how to check a user input path for dirve existence
Brian van den Broek
bvande at po-box.mcgill.ca
Mon Apr 5 19:00:04 EDT 2004
Hi all,
as described in my previous post, I am trying to write a function
which asks the user for a directory name, checks for existence,
and offers to create it if it does not.
The function asks for a directory this way:
asked_dir = raw_input("What Directory shall we work in?\n")
Since this gets a raw string, if I go from a failed existence
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.
I can think of two approaches:
1) Embed the mkdir('J:\\spam') within a try/except to catch the
OSError. Print error msg and start again if exception is caught.
2) Initialize asked_drivename to an empty string. Check if
asked_dir has a ':' (in effect, checking to see if the string
begins with a drive name), and if so, then extract the beginning
of the string up to and including the colon to asked_drivename.
Finally, if asked_drivename is non-empty, then use
os.path.exists(asked_drivename) to check if the asked_dir string
begins with a valid drive name. Process if it does, complain to
user if not.
My questions:
1) Pro's and con's of the two approaches I outline?
2) Other alternatives I've missed in my noobieness? (Wouldn't
shock me in the least!)
Thanks and best,
Brian vdB
More information about the Tutor
mailing list