How to check file existence using Python?

Small tutorial with code examples and video about how to check if file exists in Python
Sergey Siminskiy's picture
Sergey Siminskiy
10 August

There are several Python methods to check does a file exist.

1. os.path.isfile
It returns True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

import os.path

if you need to be sure it's a file.

2. Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7):

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
    # file exists

3. To check a directory, use:

if my_file.is_dir():
    # directory exists

4. To check whether a Path object exists independently of whether is it a file or directory, use exists():

if my_file.exists():
    # path exists

You can also use resolve() in a try block:

    my_abs_path = my_file.resolve():
except FileNotFoundError:
    # doesn't exist
    # exists

How to combine Python and Erlang?

Over view of Pyrlang Library, a Python library, that implements Erlang distribution protocol 
Sergey Siminskiy's picture
Sergey Siminskiy
21 August

Python is the most popular 2017 programming language, according to the IEEE Spectrum index. An interpreted language, Python has a design philosophy that emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly brackets or keywords), and a syntax that allows programmers to express concepts in fewer lines of code than might be used in languages such as C++ or Java. The language provides constructs intended to enable writing clear programs on both a small and large scale.

Erlang is a general-purpose, concurrent, functional programming language, as well as a garbage-collected runtime system. Elixir, one of the most popular "young" programming language, runs on the Erlang virtual machine (BEAM).

How to combine Python and Erlang?

For this purpose, Purlang Library will suit. 

Pyrlang is a Python library which implements Erlang distribution protocol and creates an Erlang-compatible node in your Erlang cluster.

You can send and receive messages, spawn “processes” on Python side, which will be addressable from Erlang using familiar Erlang concepts like message sending to process identifiers or registered names. Also same works from Python: you can address Erlang processes, send messages to them, monitor and link with them.

The library is designed to be dropped into existing code base with very few changes required.

Learn more at official website.