By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,532 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,532 IT Pros & Developers. It's quick & easy.

shutil.move, permission denied, why ?

P: n/a

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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
Also sprach Josiah Carlson :

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


Thanks for the idea, but unfortunately it doesnot change anything.
Jul 18 '05 #7

P: n/a
"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.