bpo-6519: Improve Python Input Output Tutorial (GH-2143) (GH-2145) · python/cpython@81c05cc (original) (raw)

`@@ -262,6 +262,35 @@ to file data is fine for text files, but will corrupt binary data like that in

`

262

262

`` :file:JPEG or :file:EXE files. Be very careful to use binary mode when

``

263

263

`reading and writing such files.

`

264

264

``

``

265

`` +

It is good practice to use the :keyword:with keyword when dealing

``

``

266

`+

with file objects. The advantage is that the file is properly closed

`

``

267

`+

after its suite finishes, even if an exception is raised at some

`

``

268

`` +

point. Using :keyword:with is also much shorter than writing

``

``

269

`` +

equivalent :keyword:try\ -\ :keyword:finally blocks::

``

``

270

+

``

271

`+

with open('workfile') as f:

`

``

272

`+

... read_data = f.read()

`

``

273

`+

f.closed

`

``

274

`+

True

`

``

275

+

``

276

`` +

If you're not using the :keyword:with keyword, then you should call

``

``

277


``f.close()`` to close the file and immediately free up any system

``

278

`+

resources used by it. If you don't explicitly close a file, Python's

`

``

279

`+

garbage collector will eventually destroy the object and close the

`

``

280

`+

open file for you, but the file may stay open for a while. Another

`

``

281

`+

risk is that different Python implementations will do this clean-up at

`

``

282

`+

different times.

`

``

283

+

``

284

`` +

After a file object is closed, either by a :keyword:with statement

``

``

285


or by calling ``f.close()``, attempts to use the file object will

``

286

`+

automatically fail. ::

`

``

287

+

``

288

`+

f.close()

`

``

289

`+

f.read()

`

``

290

`+

Traceback (most recent call last):

`

``

291

`+

File "", line 1, in

`

``

292

`+

ValueError: I/O operation on closed file

`

``

293

+

265

294

``

266

295

`.. _tut-filemethods:

`

267

296

``

``` @@ -354,27 +383,6 @@ to the very file end with seek(0, 2)) and the only valid offset values are


`354`

`383`

``` those returned from the ``f.tell()``, or zero. Any other *offset* value produces

355

384

`undefined behaviour.

`

356

385

``

357

``

-

358

``


When you're done with a file, call ``f.close()`` to close it and free up any

359

``


system resources taken up by the open file. After calling ``f.close()``,

360

``

`-

attempts to use the file object will automatically fail. ::

`

361

``

-

362

``

`-

f.close()

`

363

``

`-

f.read()

`

364

``

`-

Traceback (most recent call last):

`

365

``

`-

File "", line 1, in

`

366

``

`-

ValueError: I/O operation on closed file

`

367

``

-

368

``

`` -

It is good practice to use the :keyword:with keyword when dealing with file

``

369

``

`-

objects. This has the advantage that the file is properly closed after its

`

370

``

`-

suite finishes, even if an exception is raised on the way. It is also much

`

371

``

`` -

shorter than writing equivalent :keyword:try\ -\ :keyword:finally blocks::

``

372

``

-

373

``

`-

with open('workfile', 'r') as f:

`

374

``

`-

... read_data = f.read()

`

375

``

`-

f.closed

`

376

``

`-

True

`

377

``

-

378

386

`` File objects have some additional methods, such as :meth:~file.isatty and

``

379

387

`` :meth:~file.truncate which are less frequently used; consult the Library

``

380

388

`Reference for a complete guide to file objects.

`