shutil.move, permission denied, why ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stéphane Ninin

    shutil.move, permission denied, why ?


    Hello world,

    I am fighting with what is probably a stupid problem.

    In a wxpython GUI, here is a method which must read a file,
    and if the file is not correctly formed rename it and create a new file:

    (I have removed most of the logic of the code here, so I am not sure
    it's going to be helpful)

    def makeTree(self):
    print 'HERE WE ARE'
    f = file(self.__fil ename,'r')
    f.close()
    from shutil import move
    from os.path import join
    bakname = self.__filename + '.bak'
    print bakname
    print f.closed
    f.close()
    print 'TEST',f.closed
    move(self.__fil ename,bakname)


    Here is some of the output corresponding to this method
    (on cygwin/Windows 2000, with python 2.3.3):

    .... in makeTree:

    move(self.__fil ename,bakname)
    File "e:\Program\Pyt hon\lib\shutil. py", line 171, in move
    os.unlink(src)
    OSError: [Errno 13] Permission denied: u'C:\\home\\nat asha\\stephane\ \PYTHON
    \\Projects\\Scr ipts\\config.xm l'

    python -V
    Python 2.3.3

    I just do not understand what could cause this "permission s denied" for this
    file. Any idea of what stupid thing I could have done somewhere in the code
    that could cause that ?


    Thanks in advance for any suggestion...

    Regards,

    --
    Stephane Ninin





  • John J. Lee

    #2
    Re: shutil.move, permission denied, why ?

    "Stéphane Ninin" <stefnin@alussi nan.org> writes:
    [...][color=blue]
    > (I have removed most of the logic of the code here, so I am not sure
    > it's going to be helpful)[/color]
    [...]

    Post a short snippet of code that runs and demonstrates the problem.


    John

    Comment

    • Peter Hansen

      #3
      Re: shutil.move, permission denied, why ?

      Stéphane Ninin wrote:
      [color=blue]
      > I just do not understand what could cause this "permission s denied" for this
      > file. Any idea of what stupid thing I could have done somewhere in the code
      > that could cause that ?[/color]

      Can you do exactly the same thing successfully from the command
      line? That is, do a "move a b" with the same paths that you
      are trying to use in your code?

      Comment

      • Josiah Carlson

        #4
        Re: shutil.move, permission denied, why ?

        > def makeTree(self):[color=blue]
        > print 'HERE WE ARE'
        > f = file(self.__fil ename,'r')
        > f.close()
        > from shutil import move
        > from os.path import join
        > bakname = self.__filename + '.bak'
        > print bakname
        > print f.closed
        > f.close()
        > print 'TEST',f.closed
        > move(self.__fil ename,bakname)[/color]

        Try... os.rename(self. __filename, bakname)

        - Josiah

        Comment

        • Stéphane Ninin

          #5
          Re: shutil.move, permission denied, why ?

          Also sprach Peter Hansen :
          [color=blue]
          >[color=green]
          >> I just do not understand what could cause this "permission s denied" for
          >> this file. Any idea of what stupid thing I could have done somewhere in
          >> the code that could cause that ?[/color]
          >
          > Can you do exactly the same thing successfully from the command
          > line? That is, do a "move a b" with the same paths that you
          > are trying to use in your code?[/color]


          A short test script is working correctly, but I will try to write something
          bigger and post it here later.

          Comment

          • Stéphane Ninin

            #6
            Re: shutil.move, permission denied, why ?

            Also sprach John J. Lee :
            [color=blue][color=green]
            >> (I have removed most of the logic of the code here, so I am not sure
            >> it's going to be helpful)[/color]
            > [...]
            >
            > Post a short snippet of code that runs and demonstrates the problem.
            >[/color]

            Ok, I have reduced it, but I am afraid problem comes from PyXML...

            You must have some file called "test.xml "
            (file is empty in my test) in the same directory as the script:


            def getFileName():
            from os.path import abspath,join
            from sys import path
            cdir = abspath(path[0])
            cfile = join(cdir,'test .xml')
            return cfile

            from xml.sax.saxutil s import DefaultHandler

            class TestReader(obje ct):

            def __init__(self):
            pass

            def read(self,filen ame):

            from _xmlplus.sax.sa x2exts import XMLValParserFac tory
            parser = XMLValParserFac tory.make_parse r()

            #from xml.sax.handler import feature_namespa ces,feature_val idation
            #parser.setFeat ure(feature_nam espaces, 0)

            dh = DefaultHandler( )

            parser.setConte ntHandler(dh)
            parser.setError Handler(dh)

            f = file(filename,' r')

            try:
            parser.parse(f)
            finally:
            parser.close()
            parser.reset()
            f.close()

            return 1

            def ViewFile():
            from xml.sax._except ions import SAXParseExcepti on

            infile = getFileName()

            a = TestReader()

            try:
            print 'A0'
            pls = a.read(infile)
            except SAXParseExcepti on,e:
            print 'B1'
            pls = {}

            from shutil import move
            from os import rename

            bakname = infile + '.bak'
            move(infile,bak name)
            #rename(infile, bakname)


            def main():
            ViewFile()

            main()





            Comment

            • Stéphane Ninin

              #7
              Re: shutil.move, permission denied, why ?

              Also sprach Josiah Carlson :
              [color=blue]
              >
              > Try... os.rename(self. __filename, bakname)
              >[/color]

              Thanks for the idea, but unfortunately it doesnot change anything.


              Comment

              • Colin Brown

                #8
                Re: shutil.move, permission denied, why ?

                "Stéphane Ninin" <stefnin@alussi nan.org> wrote in message
                news:Xns94C3B42 DA41DEstefninal ussinanorg@212. 27.42.68...
                ....[color=blue]
                > I just do not understand what could cause this "permission s denied" for[/color]
                this[color=blue]
                > file. Any idea of what stupid thing I could have done somewhere in the[/color]
                code[color=blue]
                > that could cause that ?[/color]
                ....[color=blue]
                > Stephane Ninin[/color]

                Hi Stéphane

                Some time ago I had a "permission denied" problem on Windows that I finally
                tracked down to os.system duplicating open handles at the creation instance!
                The method I used to locate the problem area was by having python fire off
                an instance of handle.exe (freely available from www.sysinternals.com) when
                the permission error occurred to see what else had the file open.

                Colin Brown
                PyNZ



                Comment

                Working...