472,143 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

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

Jul 18 '05 #1
7 13552
"Stéphane Ninin" <st*****@alussinan.org> 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
Jul 18 '05 #2
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?
Jul 18 '05 #3
> 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
Jul 18 '05 #4
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.

Jul 18 '05 #5
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()

Jul 18 '05 #6
Also sprach Josiah Carlson :

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


Thanks for the idea, but unfortunately it doesnot change anything.
Jul 18 '05 #7
"Stéphane Ninin" <st*****@alussinan.org> wrote in message
news:Xn*********************************@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

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Daniel Bickett | last post: by
1 post views Thread by Todd7 | last post: by
6 posts views Thread by Antoine De Groote | last post: by
10 posts views Thread by Robert Dailey | last post: by
reply views Thread by Ahmed, Shakir | last post: by
2 posts views Thread by Lance Hoffmeyer | last post: by
4 posts views Thread by Roopesh | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.