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 use the pathlib’s glob method to get all entries in the file system from the root.

And then we can use the is_file method to check if each file is a file.

If it is, then we use the stat method to get the file size.

And we use the sum method to sum all the file sizes together.

For instance, we write:

from pathlib import Path

root_directory = Path('.')
s = sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file())
print(s)

We set root_directory to the root path object.

Then we call root_directory.glob with '**/*' to get everything under root_directory.

And we filter out non-files with if f.is_file().

Then we get the size of each file with f.stat().st_size.

And we call sum to add the file sizes together.

Therefore, s is a number in bytes like 16629.

Conclusion

To calculate a directory’s size using Python, we can use the pathlib’s glob method to get all entries in the file system from the root.

And then we can use the is_file method to check if each file is a file.

If it is, then we use the stat method to get the file size.

And we use the sum method to sum all the file sizes together.

Categories
Python Answers

How to make requests via a proxy with Python?

Sometimes, we want to make requests via a proxy with Python.

In this article, we’ll look at how to make requests via a proxy with Python.

How to make requests via a proxy with Python?

To make requests via a proxy with Python, we can use the requests module.

For instance, we write:

import requests

url = 'http://www.google.com'
proxy = '206.253.164.108'
r = requests.get(url, proxies={"http": proxy})
print(r)

We call requests.get with the url to make the request to and a dictionary with an entry with the 'http' key set to a string with the proxy’s address.

Therefore, if the request is successful, we get something like:

<Response [200]>

returned.

Conclusion

To make requests via a proxy with Python, we can use the requests module.

Categories
Python Answers

How to add N seconds to datetime.time in Python?

Sometimes, we want to add N seconds to datetime.time in Python.

In this article, we’ll look at how to add N seconds to datetime.time in Python.

How to add N seconds to datetime.time in Python?

To add N seconds to datetime.time in Python, we can add a timedelta object to a datetime.

For instance, we write:

import datetime

a = datetime.datetime(100, 2, 2, 11, 30, 59)
b = a + datetime.timedelta(0, 3)
print(a.time())
print(b.time())

We create a datetime object with a = datetime.datetime(100, 2, 2, 11, 30, 59).

And then we add 3 seconds with a + datetime.timedelta(0, 3).

We then get the time value in hours:minutes:seconds format with the time method and print the returned result.

Therefore, we have:

11:30:59
11:31:02

from the print output.

Conclusion

To add N seconds to datetime.time in Python, we can add a timedelta object to a datetime.

Categories
Python Answers

How to create a linked list with Python?

Sometimes, we want to create a linked list with Python.

In this article, we’ll look at how to create a linked list with Python.

How to create a linked list with Python?

To create a linked list with Python, we can use a deque.

For instance, we write:

from collections import deque

d = deque([1, 2, 3, 4])

print(d)
for x in d:
    print(x)
print(d.pop(), d)

We create a deque with deque([1, 2, 3, 4]).

Next, we loop through the items in d and print them.

Then we call d.pop to remove the last entry from the deque and print the new value of d.

Therefore, we see:

deque([1, 2, 3, 4])
1
2
3
4
4 deque([1, 2, 3])

printed.

Conclusion

To create a linked list with Python, we can use a deque.

Categories
Python Answers

How to execute cat subprocess in parallel with Python?

Sometimes, we want to execute cat subprocess in parallel with Python.

In this article, we’ll look at how to execute cat subprocess in parallel with Python.

How to execute cat subprocess in parallel with Python?

To execute cat subprocess in parallel with Python, we can use the subprocess module.

For instance, we write:

from subprocess import Popen

processes = [
    Popen("echo {i:d}; sleep 2; echo {i:d}".format(i=i), shell=True)
    for i in range(5)
]
exitcodes = [p.wait() for p in processes]

We call Popen with the command we want to run`.

And we set shell to True to let us use the shell.

We specify that we run the commands 5 times with for i in range(5).

Then we return exit codes for each process with [p.wait() for p in processes].

Conclusion

To execute cat subprocess in parallel with Python, we can use the subprocess module.