shutil.move, permission denied, why ?

Discussion in 'Python' started by Stéphane Ninin, Apr 6, 2004.

  1. 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.__filename,'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.__filename,bakname)


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

    .... in makeTree:

    move(self.__filename,bakname)
    File "e:\Program\Python\lib\shutil.py", line 171, in move
    os.unlink(src)
    OSError: [Errno 13] Permission denied: u'C:\\home\\natasha\\stephane\\PYTHON
    \\Projects\\Scripts\\config.xml'

    python -V
    Python 2.3.3

    I just do not understand what could cause this "permissions 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
    Stéphane Ninin, Apr 6, 2004
    #1
    1. Advertising

  2. Stéphane Ninin

    John J. Lee Guest

    "Stéphane Ninin" <> writes:
    [...]
    > (I have removed most of the logic of the code here, so I am not sure
    > it's going to be helpful)

    [...]

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


    John
    John J. Lee, Apr 6, 2004
    #2
    1. Advertising

  3. Stéphane Ninin

    Peter Hansen Guest

    Stéphane Ninin wrote:

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


    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?
    Peter Hansen, Apr 6, 2004
    #3
  4. > def makeTree(self):
    > print 'HERE WE ARE'
    > f = file(self.__filename,'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.__filename,bakname)


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

    - Josiah
    Josiah Carlson, Apr 7, 2004
    #4
  5. Also sprach Peter Hansen :

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

    >
    > 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?



    A short test script is working correctly, but I will try to write something
    bigger and post it here later.
    Stéphane Ninin, Apr 7, 2004
    #5
  6. Also sprach John J. Lee :

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

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


    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.saxutils import DefaultHandler

    class TestReader(object):

    def __init__(self):
    pass

    def read(self,filename):

    from _xmlplus.sax.sax2exts import XMLValParserFactory
    parser = XMLValParserFactory.make_parser()

    #from xml.sax.handler import feature_namespaces,feature_validation
    #parser.setFeature(feature_namespaces, 0)

    dh = DefaultHandler()

    parser.setContentHandler(dh)
    parser.setErrorHandler(dh)

    f = file(filename,'r')

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

    return 1

    def ViewFile():
    from xml.sax._exceptions import SAXParseException

    infile = getFileName()

    a = TestReader()

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

    from shutil import move
    from os import rename

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


    def main():
    ViewFile()

    main()
    Stéphane Ninin, Apr 7, 2004
    #6
  7. Also sprach Josiah Carlson :

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


    Thanks for the idea, but unfortunately it doesnot change anything.
    Stéphane Ninin, Apr 7, 2004
    #7
  8. Stéphane Ninin

    Colin Brown Guest

    "Stéphane Ninin" <> wrote in message
    news:Xns94C3B42DA41DEstefninalussinanorg@212.27.42.68...
    ....
    > I just do not understand what could cause this "permissions denied" for

    this
    > file. Any idea of what stupid thing I could have done somewhere in the

    code
    > that could cause that ?

    ....
    > Stephane Ninin


    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
    Colin Brown, Apr 7, 2004
    #8
  9. Also sprach Colin Brown :

    >
    > 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.
    >


    Thanks for the idea. It helps.

    On this part of the code:
    (see <Xns94C49F78550F2stefninalussinanorg@212.27.42.71> for full test
    script)

    def read(self,filename):

    from _xmlplus.sax.sax2exts import XMLValParserFactory
    parser = XMLValParserFactory.make_parser()

    dh = DefaultHandler()

    parser.setContentHandler(dh)
    parser.setErrorHandler(dh)

    f = file(filename,'r')


    print 'TestReader B'
    system('handle.exe xml')
    r=raw_input('Press\n')


    try:
    parser.parse(f)
    finally:
    print 'CLOSE FILE'
    parser.close()
    parser.reset()
    f.close()
    print 'TestReader C'
    system('handle.exe xml')
    r=raw_input('Press\n')
    print f.closed


    return 1


    I have the following output for a "bad file"
    (parser throws exception during parsing):


    TestReader B

    Handle v2.2
    Copyright (C) 1997-2004 Mark Russinovich
    Sysinternals - www.sysinternals.com

    python.exe pid: 1776 path to test3.xml
    CMD.EXE pid: 1304 path to test3.xml
    handle.exe pid: 1072 path to test3.xml
    Press

    CLOSE FILE
    B1

    Handle v2.2
    Copyright (C) 1997-2004 Mark Russinovich
    Sysinternals - www.sysinternals.com

    python.exe pid: 1776 path to test3.xml
    CMD.EXE pid: 1020 path to test3.xml
    handle.exe pid: 1724 path to test3.xml
    Press


    .... while, for a file which is correctly parsed I have this output:

    TestReader B

    Handle v2.2
    Copyright (C) 1997-2004 Mark Russinovich
    Sysinternals - www.sysinternals.com

    python.exe pid: 1324 path to test3.xml
    CMD.EXE pid: 1020 path to test3.xml
    handle.exe pid: 1776 path to test3.xml
    Press

    CLOSE FILE
    TestReader C

    Handle v2.2
    Copyright (C) 1997-2004 Mark Russinovich
    Sysinternals - www.sysinternals.com

    No matching handles found.
    Press

    ~~~

    I am not sure I am interpreting this correctly...
    Even if file is closed (as say f.closed),
    handles are still there ?

    And if problem comes from the parser (PyXML), I guess I will somehow
    workaround the problem, as I dont want to dig into PyXML code. :)
    (Unless you have some other idea.)
    Well, I will also test this week-end if problem also happens on Linux.
    (as this code is supposed to run only on Linux,
    I will just skip this problem if it works fine on linux)

    Thanks for your suggestion.


    Regards,

    --
    Stephane Ninin
    Stéphane Ninin, Apr 8, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,655
    Smokey Grindel
    Dec 2, 2006
  2. Antoine De Groote

    shutil: permission denied errors on windows

    Antoine De Groote, Nov 6, 2006, in forum: Python
    Replies:
    6
    Views:
    1,671
    Antoine De Groote
    Nov 7, 2006
  3. Ahmed, Shakir

    permission denied in shutil.copyfile

    Ahmed, Shakir, Jul 13, 2007, in forum: Python
    Replies:
    0
    Views:
    549
    Ahmed, Shakir
    Jul 13, 2007
  4. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    836
    alex23
    Sep 26, 2011
  5. BLIZZARDICE

    Move outside results in permission denied?

    BLIZZARDICE, Jul 11, 2006, in forum: Javascript
    Replies:
    4
    Views:
    82
    Kevin Darling
    Jul 12, 2006
Loading...

Share This Page