Categories
Python Answers

How to ping servers in Python?

Sometimes, we want to ping servers in Python.

In this article, we’ll look at how to ping servers in Python.

How to ping servers in Python?

To ping servers in Python, we can run the ping command with subprocess.

For instance, we write

import platform   
import subprocess 

def ping(host):
    param = '-n' if platform.system().lower()=='windows' else '-c'
    command = ['ping', param, '1', host]
    return subprocess.call(command) == 0

to define the ping function.

In it, we get the ping param given the platform name with

param = '-n' if platform.system().lower()=='windows' else '-c'

Then we create the command list with the command and the arguments for it.

Next, we call subprocess.call with the command and check if it has a 0 exit code with ==.

Conclusion

To ping servers in Python, we can run the ping command with subprocess.

Categories
Python Answers

How to find a file in Python?

Sometimes, we want to find a file in Python.

In this article, we’ll look at how to find a file in Python.

How to find a file in Python?

To find a file in Python, we can use the os.walk method.

For instance, we write

import os

def find(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)

to define the find function to find the file with name in the path.

In it, we call os.walk to loop through the files in path in all child directories of path.

And we check if name is in files.

If it is, we return the path of the file with name with

os.path.join(root, name)

Conclusion

To find a file in Python, we can use the os.walk method.

Categories
Python Answers

How to calculate a directory’s size using Python?

Sometimes, we want to calculate a directory’s size using Python.

In this article, we’ll look at how to calculate a directory’s size using Python.

How to calculate a directory’s size using Python?

To calculate a directory’s size using Python, we can loop through each file in the directory and get a sum of their sizes.

For instance, we write

import os

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                total_size += os.path.getsize(fp)
    return total_size

print(get_size(), 'bytes')

to call os.walk to get an iterator with the files in all child directories of start_path.

Then we loop through them with a for loop.

In it, we loop through the filenames returned.

Then we get the file paths with

fp = os.path.join(dirpath, f)

We check if the path is a symbolic link with islink.

If it’s not, then we get the file’s size with getsize and add the size in bytes to total_size.

Once we’re done, we return total_size in bytes.

Conclusion

To calculate a directory’s size using Python, we can loop through each file in the directory and get a sum of their sizes.

Categories
Python Answers

How to convert a UTC datetime to a local datetime using only standard library with Python?

Sometimes, we want to convert a UTC datetime to a local datetime using only standard library with Python.

In this article, we’ll look at how to convert a UTC datetime to a local datetime using only standard library with Python.

How to convert a UTC datetime to a local datetime using only standard library with Python?

To convert a UTC datetime to a local datetime using only standard library with Python, we can use the pytz module.

For instance, we write

import pytz

local_tz = pytz.timezone('Europe/London')


def utc_to_local(utc_dt):
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    return local_tz.normalize(local_dt)

to create the utc_to_local function that takes the utc_dt datetime.

In it, we call utc_dt.replace with the tzinfo argument set to pytz.utc to parse the datetime as UTC.

Then we call astimezone with local_tz to convert the UTC datetime to the local_tz time zone.

And then we call local_tz.normalize with the local_dt to return the local datetime.

Conclusion

To convert a UTC datetime to a local datetime using only standard library with Python, we can use the pytz module.

Categories
Python Answers

How to get the path of the current executed file in Python?

Sometimes, we want to get the path of the current executed file in Python.

In this article, we’ll look at how to get the path of the current executed file in Python.

How to get the path of the current executed file in Python?

To get the path of the current executed file in Python, we cajn use the inspect and os modules.

For instance, we write

from inspect import getsourcefile
from os.path import abspath

p = abspath(getsourcefile(lambda: 0))

to call getsourcefile to get the currently executing file.

We call it with a function that returns 0.

And then we call abspath to get the absolute path of the path returned by getsourcefile to get the full path.

Conclusion

To get the path of the current executed file in Python, we cajn use the inspect and os modules.