shutil.move, permission denied, why ?

  • Thread starter Stéphane Ninin
  • Start date
S

Stéphane Ninin

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,
 
J

John J. Lee

Stéphane Ninin said:
(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
 
P

Peter Hansen

Stéphane Ninin said:
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?
 
J

Josiah Carlson

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
 
S

Stéphane Ninin

Also sprach Peter Hansen :
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.
 
S

Stéphane Ninin

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()
 
C

Colin Brown

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

Stéphane Ninin

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 <[email protected]> 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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top