[Tutor] Hello World in Python without space

Richard D. Moores rdmoores at gmail.com
Fri Jul 15 23:21:38 CEST 2011


On Sun, Jul 10, 2011 at 05:05, Peter Otten <__peter__ at web.de> wrote:

> >>> help(print)
>
> shows
>
> print(...)
>    print(value, ..., sep=' ', end='\n', file=sys.stdout)
>
>    Prints the values to a stream, or to sys.stdout by default.
>    Optional keyword arguments:
>    file: a file-like object (stream); defaults to the current sys.stdout.
>    sep:  string inserted between values, default a space.
>    end:  string appended after the last value, default a newline.

I didn't know that printing to a file with print() was possible, so I tried

>>> print("Hello, world!", file="C:\test\test.txt")
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
builtins.AttributeError: 'str' object has no attribute 'write'
>>>

And the docs at
<http://docs.python.org/py3k/library/functions.html#print> tell me
"The file argument must be an object with a write(string) method; if
it is not present or None, sys.stdout will be used."

What do I do to test.txt to make it "an object with a write(string) method"?

Thanks,

Dick Moores


More information about the Tutor mailing list