[Tutor] open(file, 'rw') problem
Dave S
pythontut at pusspaws.net
Thu Jul 27 08:50:05 CEST 2006
Good morning,
I am having a problem with file open, read & write. The example shows my
problem. I would have expected it to print a sequence 1,2,3,4 incrementing
every time it is run.
#!/usr/bin/env python
import os, shutil, time
basher_dir = '/home/dave/PodCasts'
bit_bucket = basher_dir + '/xbit_bucket'
cd_count = bit_bucket + '/xcd_count'
if not os.path.isfile(cd_count):
f = open(cd_count, 'w')
f.write('0')
f.close()
f = open(cd_count,'rw')
n = int(f.read())
n += 1
print n
f.seek(0)
f.write(str(n))
f.close()
Instead I get ...
dave at dave-comp:~/my_files/python_develop/unison/podsplit$ ./test.py
1
Traceback (most recent call last):
File "./test.py", line 20, in ?
f.write(str(n))
IOError: [Errno 9] Bad file descriptor
dave at dave-comp:~/my_files/python_develop/unison/podsplit$
It appears to be a problem with me opening the file as 'rw' although I have
googled and opening as 'rw' appears legal. Can you only open a file as 'r'
or 'w' - ie open & close the file twice ?
Any ideas
Cheers
Dave
More information about the Tutor
mailing list