Python Doc Error: os.makedirs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    Python Doc Error: os.makedirs

    Python doc problem:

    The official home of the Python Programming Language


    makedirs( path[, mode])
    Recursive directory creation function. Like mkdir(), but makes all
    intermediate-level directories needed to contain the leaf directory.
    Throws an error exception if the leaf directory already exists or
    cannot be created. The default mode is 0777 (octal). This function does
    not properly handle UNC paths (only relevant on Windows systems;
    Universal Naming Convention paths are those that use the `\\host\path'
    syntax). New in version 1.5.2.


    The “Throws an error exception” should be “Throws an OSError
    exception”.

    --------

    i think the function shouldn't complain if dir already exists. How is a
    programer to distinguish if the dir already exists, or if there's a
    problem creating the dir?


    Xah
    xah@xahlee.org
    ∑ http://xahlee.org/

  • pclinch@internet-glue.co.uk

    #2
    Re: Python Doc Error: os.makedirs

    if os.access( path, os.F_OK):
    print 'path exists'

    See also os.stat(path) for further info. about a file or dir.

    regards, Paul Clinch

    Comment

    • Dr. Who

      #3
      Re: Python Doc Error: os.makedirs


      Xah Lee wrote:[color=blue]
      > i think the function shouldn't complain if dir already exists. How is a
      > programer to distinguish if the dir already exists, or if there's a
      > problem creating the dir?[/color]

      Of course it should thrown an exception because it was unable to do
      what it was asked: create a directory. The fact that the directory
      already exists is irrelevant to the function...it still failed to
      create the directory.

      And I have had situations where attempting to create a directory that
      already exists was an error condition.

      Jeff

      Comment

      • the_crazy88

        #4
        Re: Python Doc Error: os.makedirs

        if not os.path.isdir(" your_dir_name") :
        makedirs("your_ dir_name")

        Comment

        • Thomas Bellman

          #5
          Re: Python Doc Error: os.makedirs

          "Xah Lee" <xah@xahlee.org > wrote:
          [color=blue]
          > The "Throws an error exception" should be "Throws an OSError
          > exception".[/color]

          Both are correct:
          [color=blue][color=green][color=darkred]
          >>> os.error is OSError[/color][/color][/color]
          True

          That is even documented in http://python.org/doc/lib/module-os.html:

          error
          This exception is raised when a function returns a system-related
          error (not for illegal argument types or other incidental errors).
          This is also known as the built-in exception OSError.
          [color=blue]
          > i think the function shouldn't complain if dir already exists. How is a
          > programer to distinguish if the dir already exists, or if there's a
          > problem creating the dir?[/color]

          Also in the documentation about os.error:

          The accompanying value is a pair containing the numeric error
          code from errno and the corresponding string, as would be printed
          by the C function perror(). See the module errno, which contains
          names for the error codes defined by the underlying operating
          system.

          When exceptions are classes, this exception carries two attributes,
          errno and strerror. The first holds the value of the C errno
          variable, and the latter holds the corresponding error message
          from strerror(). For exceptions that involve a file system path
          (such as chdir() or unlink()), the exception instance will
          contain a third attribute, filename, which is the file name
          passed to the function.

          Thus, this gives you the behaviour you want:

          try:
          os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
          except os.error, e:
          if e.errno != errno.EEXIST:
          raise


          --
          Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
          "I refuse to have a battle of wits with an ! bellman @ lysator.liu.se
          unarmed person." ! Make Love -- Nicht Wahr!

          Comment

          • Thomas Bellman

            #6
            Re: Python Doc Error: os.makedirs

            I wrote:
            [color=blue]
            > try:
            > os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
            > except os.error, e:
            > if e.errno != errno.EEXIST:
            > raise[/color]

            Actually, when I think more about it, one would probably rather
            want something like:

            try:
            os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
            except os.error, e:
            if ( e.errno != errno.EEXIST or
            not os.path.isdir("/tmp/trh/spam/norwegian/blue/parrot/cheese")):
            raise


            --
            Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
            "Beware of bugs in the above code; I have ! bellman @ lysator.liu.se
            only proved it correct, not tried it." ! Make Love -- Nicht Wahr!

            Comment

            • Xah Lee

              #7
              Re: Python Doc Error: os.makedirs


              Thomas Bellman wrote:[color=blue]
              > try:
              > os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
              > except os.error, e:
              > if e.errno != errno.EEXIST:
              > raise[/color]

              This is what i want. Thanks.

              (the doc needs quite some improvement...)

              Xah
              xah@xahlee.org
              ∑ http://xahlee.org/

              Comment

              • jepler@unpythonic.net

                #8
                os.makedirs should not succeed when the directory already exists (was

                On Wed, Oct 19, 2005 at 09:26:16AM -0700, Dr. Who wrote:[color=blue]
                > The fact that the directory already exists is irrelevant to the function...it
                > still failed to create the directory.[/color]

                That's not true. Imagine that os.makedirs() is used inside tempfile.mkdtem p()
                (I looked, and it isn't) and the proposed behavior (do not raise an exception
                when the directory already exists) is adopted.

                In this case, there is a race condition between you and the attacker who
                guesses the next directory you will attempt to make. If he calls mkdir()
                before you do, then your os.makedirs() returns successfully (instead of raising
                an exception) and you place your files into a location that is under the
                control of someone else.

                If the attacker then makes the directory setuid himself, that files created in
                the directory are owned by him. Now, he can view and change the contents of
                these files. This can lead to a local priviledge escalation.

                Errors should never pass silently.
                Unless explicitly silenced.
                -- from the Zen of Python ('import this')
                .... and wanting them to do so may introduce a security bug in your software.

                If you know more about your users and their environments than I do (for
                instance, that none of them will ever use a multi-user computer system) maybe
                you should choose to wrap os.makedirs with something that silences EEXIST.
                But I'm glad Python does the secure thing and treats EEXIST as a failure by default.

                Jeff

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.1 (GNU/Linux)

                iD8DBQFDWDrkJd0 1MZaTXX0RAoaOAJ 9W1IlFBH7lFLC/izH2S1euV87xGgC ggR81
                eeK3wBR5X6DvbXZ cIX0NMoA=
                =dzVp
                -----END PGP SIGNATURE-----

                Comment

                Working...