Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is this a directory?
#1
there appears to be many ways to test if i have a reference to a directory (as opposed to something else like a file or a path that does not exist).  which method of testing should i not use?  is there a reason why not?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
When you say "a reference to a directory" do you mean a string? It's hard to say without more details, but it's typically Pythonic to "ask for forgiveness rather than permission" meaning it's a good idea to try use the path as a directory, and catch an exception if what you tried to do fails. Why that instead of "look before you leap"? Technically, circumstances can change between when you check and when you try, so you should theoretically do all the exception handling even if you do the checking, so the checking isn't so worth bothering with.
Reply
#3
sorry, my head slipped into that old box again. gotta be thinking more pythonic.

rewriting this and rethinking it.

what the script needs to do is make sure a directory is there, given a path in a string.  i just recoded it to make the directory.  that will fail if something was already there.  it will usually be a directory, previously created.  that is ok if so.  it might be something else like a regular file.  that is bad and we must not go on.  this script does nothing with the directory, it invoke another program that will use the directory or fail badly if there is not a directory there.  so my thought is to test if it is a directory by using it in some benign way like cd into it and cd back out.  how pythonic is this?  the path is obtained from a command environment passed along to the next program.  this script is being made to run before that next program to modify and improve the startup conditions, such as making sure the logging directory exists.  if it fails to cd into that directory it will delete important files from the current directory, so i want to be as sure as possible that it will succeed.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
The pythonic way is:
import os.path

if os.path.isdir('/some/path')
But if you need to make sure you can get there, os.chdir('/some/path') with a try/excep is a good as it gets.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
ok, i will mkdir then os.path.isdir().  if os.path.isdir() indicates not a directory, the i will abort.  simple enough.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020