Dlaczego próba drukowania bezpośrednio do pliku zamiast sys.stdoutgenerowania następującego błędu składniowego:
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f1=open('./testfile', 'w+')
>>> print('This is a test', file=f1)
File "<stdin>", line 1
print('This is a test', file=f1)
^
SyntaxError: invalid syntax
Z pomocy (__ builtins__) mam następujące informacje:
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.
Jaka byłaby zatem właściwa składnia do zmiany standardowych zapisów strumieniowych?
Wiem, że istnieją inne, być może lepsze sposoby zapisywania do pliku, ale naprawdę nie rozumiem, dlaczego powinien to być błąd składni ...
Byłoby mile widziane miłe wyjaśnienie!
print()jest funkcją wbudowaną w pythonie 3.x, natomiastprintoperatorem jest python <3.x. Post pokazuje2.7.2+.