File Operations in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 4: Line 4:
=Check whether a File Exists=
=Check whether a File Exists=
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
from os.path import exists
import os.path
file_exists = exists(path_to_file)
file_exists = os.path.exists(path_to_file)
</syntaxhighlight>
</syntaxhighlight>
Returns <code>True</code> or <code>False</code>.
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
from pathlib import Path
from pathlib import Path

Revision as of 23:48, 20 January 2022

Internal

Check whether a File Exists

import os.path
file_exists = os.path.exists(path_to_file)

Returns True or False.

from pathlib import Path
path = Path(path_to_file)
path.is_file()