Discussion:
[newbie] Writing to file : "name 'concat' is not defined"?
Byron
2004-07-21 15:49:28 UTC
Permalink
Hi Fred,

It looks like they have answered your questions. To reiterate in a
simple form, to open a file for reading, it is best to use something
like the following:

f = open("c:/test.txt", "r")
textlines = f.readlines() # Reads all lines into a list (array).
for line in textlines: # Prints each line that is read from the
text file.
print line

---

If you want to append (add additional information) to a text file, use
the following:

f = open("c:/test.txt", "a")
f.write("This is an appended line.\n")
f.close()


Hope this helps,

Byron
------------------------
Hi,
I've searched the web and the archives of this ng for half an
hour already, and I'm still stuck.
1. On a W2K Pro host, installed the latest ActivePython MSI. Rebooted.
file=concat('test.txt','w')
file.close()
"NameError: name 'concat' is not defined"
- from operator import * -> "AttributeError: 'str' object has no
attribute 'close'"
- import operator -> "NameError: name 'concat' is not defined"
- file=concat('./test.txt','w')
- file=concat('.\test.txt','w')
... all to no avail. Obviously, I'm either missing a package or I need
to tweak something. Any idea?
Thank you
Fred.
Nigel Rowe
2004-07-21 15:26:02 UTC
Permalink
On Wed, 21 Jul 2004 16:01:04 +0200, Brian Quinlan <brian at sweetapp.com>
I don't see any reference to a "concat" function in either of those
resources.
Er...
Dive into Python : "Example 6.3. Opening a File" at the very
beginning. No mention of an "open" statement, just concat. Same lower
in "Example 6.7. Writing to Files".
<snip>
And in the Python Tutorial, jump to "7.2 Reading and Writing Files"
The text mentions open(), but the samples don't show it, and use
concat() instead.
Where is the usage of concat?
Cheers,
Brian
Ahhh, I've seen this before. I think the OP is browsing "Dive into Python",
by way of an old version of privoxy. By default its popup blocking
functions used to rewrite "window.open(..." as something like (from memory)
"concat(window, ....".

The newer verions rewrite it as "PrivoxyWindowOpen(..." which is at least a
bit more recognisable.
--
Nigel Rowe
A pox upon the spammers that make me write my address like..
rho (snail) swiftdsl (stop) com (stop) au
Fred
2004-07-21 16:35:17 UTC
Permalink
On Thu, 22 Jul 2004 01:26:02 +1000, Nigel Rowe
Post by Nigel Rowe
The newer verions rewrite it as "PrivoxyWindowOpen(..." which is at least a
bit more recognisable.
You guys are sharp :-) For those bitten by the same "feature", make
sure you download the latest release of Privoxy
(privoxy_setup_3_0_3-2.exe as of this writing). Do NOT copy/paster
your config/filter files, as those that come with Privoxy have
changed. Instead, you'll have to copy/paster your personal filters
into the latest versions of those files as installed by Privoxy.

Then, instead of seeing open() replaced with concat(), you'll see:

f=PrivoxyWindowOpen('/tmp/workfile', 'w')

MMmm :-)

Thx a bunch
Fred.
Brian Quinlan
2004-07-21 15:12:43 UTC
Permalink
Perhaps some sort of web-proxy is rewriting stuff?
Example 6.3. Opening a File
f = PrivoxyWindowOpen("/music/_singles/kairo.mp3", "rb")
f
Looks like it:
http://www.privoxy.org/faq/trouble.html#WINDOWOPEN

I think that this behavior (i.e. modifying the document content) is
incredibly evil.

Cheers,
Brian
Fred
2004-07-21 13:48:37 UTC
Permalink
On Wed, 21 Jul 2004 15:31:00 +0200, Brian Quinlan <brian at sweetapp.com>
Think about these two lines of code for a minute...what do you expect
file = open('test.txt', 'w')
file.close()
f = open('test.txt', 'w')
f.close()
Thank you Brian. That solved it :-)

However, the above was just stuff I read on the web:

http://diveintopython.org/file_handling/file_objects.html#d0e15055
http://docs.python.org/tut/node9.html

Thx again
Fred.
Brian Quinlan
2004-07-21 14:37:48 UTC
Permalink
On Wed, 21 Jul 2004 16:01:04 +0200, Brian Quinlan <brian at sweetapp.com>
I don't see any reference to a "concat" function in either of those
resources.
Er...
Dive into Python : "Example 6.3. Opening a File" at the very
beginning. No mention of an "open" statement, just concat. Same lower
in "Example 6.7. Writing to Files".
Example 6.3. Opening a File
f = open("/music/_singles/kairo.mp3", "rb")
f
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
f.mode
'rb'
f.name
'/music/_singles/kairo.mp3'

Example 6.7. Writing to Files
logfile = open('test.log', 'w')
logfile.write('test succeeded')
logfile.close()
print file('test.log').read()
test succeeded
logfile = open('test.log', 'a')
logfile.write('line 2')
logfile.close()
print file('test.log').read()
test succeededline 2
And in the Python Tutorial, jump to "7.2 Reading and Writing Files"
The text mentions open(), but the samples don't show it, and use
concat() instead.
Where is the usage of concat?

Cheers,
Brian
Glenn Andreas
2004-07-21 15:03:06 UTC
Permalink
In article <mailman.663.1090420427.5135.python-list at python.org>,
On Wed, 21 Jul 2004 16:01:04 +0200, Brian Quinlan <brian at sweetapp.com>
I don't see any reference to a "concat" function in either of those
resources.
Er...
Dive into Python : "Example 6.3. Opening a File" at the very
beginning. No mention of an "open" statement, just concat. Same lower
in "Example 6.7. Writing to Files".
Example 6.3. Opening a File
f = open("/music/_singles/kairo.mp3", "rb")
f
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
f.mode
'rb'
f.name
'/music/_singles/kairo.mp3'
Example 6.7. Writing to Files
logfile = open('test.log', 'w')
logfile.write('test succeeded')
logfile.close()
print file('test.log').read()
test succeeded
logfile = open('test.log', 'a')
logfile.write('line 2')
logfile.close()
print file('test.log').read()
test succeededline 2
And in the Python Tutorial, jump to "7.2 Reading and Writing Files"
The text mentions open(), but the samples don't show it, and use
concat() instead.
Where is the usage of concat?
Perhaps some sort of web-proxy is rewriting stuff?

If I look at the same thing (and I'm running Privoxy) I get:


Example 6.3. Opening a File
f = PrivoxyWindowOpen("/music/_singles/kairo.mp3", "rb")
f
<open file '/music/_singles/kairo.mp3', mode 'rb' at 010E3988>
f.mode
'rb'
f.name
'/music/_singles/kairo.mp3'

Example 6.7. Writing to Files
logfile = PrivoxyWindowOpen('test.log', 'w')
logfile.write('test succeeded')
logfile.close()
print file('test.log').read()
test succeeded
logfile = PrivoxyWindowOpen('test.log', 'a')
logfile.write('line 2')
logfile.close()
print file('test.log').read()
test succeededline 2
Fred
2004-07-21 15:12:19 UTC
Permalink
On Wed, 21 Jul 2004 10:03:06 -0500, Glenn Andreas <gandreas at no.reply>
Perhaps some sort of web-proxy is rewriting stuff?
Damn!!!!! I had no idea Privoxy would do this in pure HTML sections!
It had me fooled big time :-D

Sorry Brian for the inconvenience
Fred.
Brian Quinlan
2004-07-21 14:01:04 UTC
Permalink
Post by Fred
Thank you Brian. That solved it :-)
http://diveintopython.org/file_handling/file_objects.html#d0e15055
http://docs.python.org/tut/node9.html
Here was your original code:

file=concat('test.txt','w')
file.close()

I don't see any reference to a "concat" function in either of those
resources.

Please let the list know if you have any other questions!

Cheers,
Brian
Fred
2004-07-21 14:30:40 UTC
Permalink
On Wed, 21 Jul 2004 16:01:04 +0200, Brian Quinlan <brian at sweetapp.com>
I don't see any reference to a "concat" function in either of those
resources.
Er...

Dive into Python : "Example 6.3. Opening a File" at the very
beginning. No mention of an "open" statement, just concat. Same lower
in "Example 6.7. Writing to Files".

And in the Python Tutorial, jump to "7.2 Reading and Writing Files"
The text mentions open(), but the samples don't show it, and use
concat() instead.

So I figured concat() was the way to go :-)

Thx again
Fred.
Fred
2004-07-21 13:15:10 UTC
Permalink
Hi,

I've searched the web and the archives of this ng for half an
hour already, and I'm still stuck.

1. On a W2K Pro host, installed the latest ActivePython MSI. Rebooted.
2. Create test.py with the following:

file=concat('test.txt','w')
file.close()

3. Open a DOS box, and type either "test.py" or "python test.py":

"NameError: name 'concat' is not defined"

I tried the following:

- from operator import * -> "AttributeError: 'str' object has no
attribute 'close'"
- import operator -> "NameError: name 'concat' is not defined"
- file=concat('./test.txt','w')
- file=concat('.\test.txt','w')

... all to no avail. Obviously, I'm either missing a package or I need
to tweak something. Any idea?

Thank you
Fred.
Brian Quinlan
2004-07-21 13:31:00 UTC
Permalink
Hi,
I've searched the web and the archives of this ng for half an
hour already, and I'm still stuck.
1. On a W2K Pro host, installed the latest ActivePython MSI. Rebooted.
file=concat('test.txt','w')
file.close()
Think about these two lines of code for a minute...what do you expect
"concat" to return? A file object? Maybe this would help:

file = open('test.txt', 'w')
file.close()

Or, even better (since "file" is a builtin object):

f = open('test'.txt', 'w')
f.close()

Cheers,
Brian
Fred
2004-07-21 16:43:04 UTC
Permalink
On Wed, 21 Jul 2004 15:49:28 GMT, Byron <DesertLinux at netscape.net>
Post by Byron
Hope this helps,
Sure does :-) Copy/paster for later use.

Thank you
Fred.

Loading...