Working with multiple files

preview_player
Показать описание
In this video, we will use the Path class from Python's pathlib module to retrieve and open multiple files stored in a directory.

Рекомендации по теме
Комментарии
Автор

You shouldn't be opening and closing files manually. An important Python concept is the Context Manager which manages stuff for you. The keyword 'with' is used with managers.

with open(<filepath>, 'r') as fh:
contents = fh.read()

This code will close the file no matter what happens within the code block. If you manually call file.close() and there is an error before you get to that line the file won't be closed. What's happening is that open returns an object that is being managed and is temporarily given the name 'fh' (for filehandle). You can give it any name you want. If the code block exits for any reason the manager will invoke the __exit__() method on the object that implements the context manager api, which in this case will close the file for you.

iandanforth
Автор

Maybe it would be nice to add that for Windows you'll need
!del data\mod*

mvs
Автор

Hi, , i wanna ask, , how about audio file? not just text file?

JosGandosKotosKotos