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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doesn't like 'newtext.txt'

Status
Not open for further replies.

Grant

Programmer
Apr 10, 1999
105
CA
Hi,

This is something I thought was a bit strange. It's really quite trivial, but just the same, I'm curious if anybody knows why this is happening.

I run the following script referencing the file 'newtext.txt' and an exception is raised:


class myfile:
def __init__(self, file):
self.myout=open(file,"w")

def writetext(self, text):
self.text=text
self.myout.write(self.text)

def __del__(self):
self.myout.close

x=myfile("C:\My Documents\My Python Scripts\newtext.txt")
x.writetext("This is the dawning of the age of Aquarius\n")
x=None




The following is the exception message:

Traceback (most recent call last):
File "C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 301, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\My Python Scripts\myout.py", line 12, in ?
x=myfile("C:\My Documents\My Python Scripts\newtext.txt")
File "C:\My Documents\My Python Scripts\myout.py", line 3, in __init__
self.myout=open(file,"w")
IOError: [Errno 2] No such file or directory: 'C:\\My Documents\\My Python Scripts\newtext.txt'
>>> Exception exceptions.AttributeError: &quot;myfile instance has no attribute 'myout'&quot; in <bound method myfile.__del__ of <__main__.myfile instance at 0x00F3D4B0>> ignored




The strange thing is, all I have to do is to change the name newtext.txt to anything else, eg: 'mytext.txt', 'dewtext.txt', etc., and it will work. (BTW, that's the only change I made.) I did a 'find' and there is definitely no file called 'newtext.txt' anywhere on my pc.

I am new to Python, but this seems goofy and rather arbitrary to me. Does anybody care to speculate why this might happen?

Thanks,
Grant.
 
mmm... strange.

I did a copy-and-paste of your code and it worked for me.
(Python 2.2.1 from ActiveState)


Although I would replace:
self.myout.close
with
self.myout.close()

the rest of the program looks correct to me, and my Python version does not complain. The file is properly written.


Did you check the existence of the directory you are writing to ?
Did you check the access rights on this directory ?
If the file already exists, do you have write permissions to it ?
Is the file opened by another application ?

 
Hi sebsauvage,

I am running ActivePython 2.2.1 build 222.

As I mentioned, the file absolutely does not exist anywhere on my pc.

It seems to me that access rights on the directory should not be an issue since, if I change the name of the file but use the same directory, I have no problems.

I also tried checking if I could create the same document using textpad. It did not complain. (I subsequently deleted it.)

I wondered if there were some explicit code somewhere in the python library that refers to this file name. I did find references to the term 'newText', but these were variable names, not hard-coded strings.


The fact that it works on your pc with no problem is significant.

It really is more of a puzzle than a problem. But what an odd thing! I would sure like to know how to troubleshoot this.

Thanks,
Grant.
 
I noticed that you didn't unescape backslashes:
&quot;C:\My Documents\My Python Scripts\newtext.txt&quot;
would rather be:
&quot;C:\\My Documents\\My Python Scripts\\newtext.txt&quot;

because Python considers \n a new line.

>>> print &quot;This a \new text&quot;
This a
ew text
>>>

See
 
Hi Sebsauvage,

Either you are a genius or I am a dope! (I think it's better for both of us if we go with the 'you are a genius' theory.)

That's exactly what the problem is.

Nice catch! Je te remercie.

Grant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top