[Tutor] os.rename() on Win 98
Francois Granger
fgranger@altern.org
Fri, 26 Jul 2002 17:42:12 +0200
I got serious difficulties with something apparently simple. so I
simplified it to trace it back as follow.
I have the following script:
=================================================Start
def saveWithBackup(data, fileName, backup = 2):
import os
dir,file = os.path.split(fileName)
parts = file.split('.')
i = 0
#filebak = os.path.join(dir, '.'.join(parts[0:-1]) + '.bak')
filebak = os.path.join(dir,
parts[0]+str(i)+'.'+'.'.join(parts[1:-1])+'.bak')
os.rename(fileName, filebak)
fileName = 'default.fr.txt'
fp = open(fileName)
data = fp.read()
fp.close
result = saveWithBackup(data, fileName, backup = 2)
print result
=================================================End
It runs fine on a Mac with MacOS 9
I run it on Windows 98: transcript of the dos session follows.
=================================================Start
C:\toto>dir
Le volume dans le lecteur C est REALPC
Le numÈro de sÈrie du volume est F400-4086
RÈpertoire de C:\toto
. <REP> 26/07/02 17:00 .
.. <REP> 26/07/02 17:00 ..
DEFAUL~1 BAK 16 908 26/07/02 11:19 default.fr.bak
DEFAUL~1 TXT 16 908 26/07/02 11:27 default.fr.txt
SAVE PY 1 456 26/07/02 16:44 save.py
SAVE1 PY 474 26/07/02 17:14 save1.py
4 fichier(s) 35 746 octets
2 rÈpertoire(s) 690 978 816 octets libres
C:\toto>c:\Python22\python save1.py
Traceback (most recent call last):
File "save1.py", line 14, in ?
result = saveWithBackup(data, fileName, backup = 2)
File "save1.py", line 8, in saveWithBackup
os.rename(fileName, filebak)
OSError: [Errno 13] Permission denied
C:\toto>
=================================================End
Not understanding what is happening, I created a simple brute force
rename wich works.
def rename(a,b):
data = open(a, 'rb').read()
open(b, 'wb').write(data)
My guess is that os.rename() does not handle properly the long name
to short name under W98 because the filebak name I creat would
produce the same short name as the one already existing ?????