Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open for append ('a')

Status
Not open for further replies.

Bong

Programmer
Dec 22, 1999
2,063
US
I can successfully open a file for reading ('r') or writing ('w') but I'm trying to open for updating ('a') and I can't get it to work.

I set a variable, outfile to 'x:/projmn/time.txt'. This file (and path) exists.
I then do:
Code:
try: f = open(outfile,'a')
except:
        a=raw_input("could not open "+outfile+" (press Enter).")
        exit()

and I get the error. I've also tried 'a+', and 'r+' with the same problem. I'm running on Windows XP with Python 2.4. Is there something I'm doing wrong?

_________________
Bob Rashkin
 
> ..and I get the error
Which error ? Post it and we will see.
 
Your example works with me without problem. For example I have this file named myfile.txt
Code:
line 1
line 2
and the following code
Code:
outfile="myfile.txt"
try: 
  f = open(outfile,'r+')
except:
  a=raw_input("could not open "+outfile+" (press Enter).")
  exit()
# now reading  
lines=f.readlines()
for line in lines:
  print line
# now appending
f.write("\nNow appending this at end")
f.close()
After running the script, it first prints the 2 lines and then appends the 3rd line, so the resulting myfile.txt is:
Code:
line 1
line 2
Now appending this at end
If you try to open the file with
Code:
f = open(outfile,'a')
then you cannot read the file, you can only append somthing at the end of file. If you try to read the file, you get an error:
Code:
Traceback (most recent call last):
  File "C:\Users\Roman\Work\pok2.py", line 8, in <module>
    lines=f.readlines()
IOError: [Errno 9] Bad file descriptor
So you need to comment-out the reading and then appending works:
Code:
outfile="myfile.txt"
try: 
  f = open(outfile,'a')
except:
  a=raw_input("could not open "+outfile+" (press Enter).")
  exit()
# now reading  
#lines=f.readlines()
#for line in lines:
#  print line
# now appending
f.write("\nNow appending this at end")
f.close()
 
It's actually rather strange. The error I get is
I/O error(2): No such file or directory
could not open x:/projMn/time.txt
However, when I execute the exact same statement (f=open('x:/projMn/time.txt','a')) interactively from a shell, I get no error.

_________________
Bob Rashkin
 
OK.
Problem solved. I was reading the filename in incorrectly.
Thanks for the help.

_________________
Bob Rashkin
 
I can't believe it, how it is possible. If you have treated the exception as showed above i.e
Code:
outfile='x:/projMn/time.txt'
try: 
  f = open(outfile,'a')
except:
  a=raw_input("could not open "+outfile+" (press Enter).")
  exit()
and the path 'x:/projMn/time.txt' not exists, you should get this message
Code:
could not open x:/projMn/time.txt (press Enter).

If the path exists and the file don't exists, then the file will be created.

Try to change the path to file
from
Code:
outfile='x:/projMn/time.txt'
to this form
Code:
outfile='x:\\projMn\\time.txt'
 
As I said, I was reading in the file name incorrectly. I didn't realize that the appended '\n' was there and needed to be stripped. Doing so fixed everything.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top