Categories
Python Answers

How to iterate over files in a given directory with Python?

Sometimes, we want to iterate over files in a given directory with Python.

In this article, we’ll look at how to iterate over files in a given directory with Python.

How to iterate over files in a given directory with Python?

To iterate over files in a given directory with Python, we can use the os.listdir method.

For instance, we write

import os

directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith('.asm') or filename.endswith('.py'):
        print(os.path.join(directory, filename))

to call os.list to list the items in the directory.

Then we call os.path.join in the loop to join the directory and filename into the full path of the file path being iterated through.

Conclusion

To iterate over files in a given directory with Python, we can use the os.listdir method.

Categories
Python Answers

How to resize an image using PIL and maintain its aspect ratio with Python?

Sometimes, we want to resize an image using PIL and maintain its aspect ratio with Python.

In this article, we’ll look at how to resize an image using PIL and maintain its aspect ratio with Python.

How to resize an image using PIL and maintain its aspect ratio with Python?

To resize an image using PIL and maintain its aspect ratio with Python, we can use the resize method.

For instance, we write

from PIL import Image

basewidth = 300
img = Image.open('somepic.jpg')
wpercent = basewidth / float(img.size[0])
hsize = int(float(img.size[1]) * float(wpercent))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
img.save('somepic.jpg')

to open the image at the given path with Image.open.

Then we calculate the proportion to scale the image by with

basewidth / float(img.size[0])

Next we calculate the new height with

int(float(img.size[1]) * float(wpercent))

Then we call img.resize with the new dimensions and assign the returned image object back to img.

And then we call save to save the resized image.

Conclusion

To resize an image using PIL and maintain its aspect ratio with Python, we can use the resize method.

Categories
Python Answers

How to find the location of my Python site-packages directory?

Sometimes, we want to find the location of my Python site-packages directory.

In this article, we’ll look at how to find the location of my Python site-packages directory.

How to find the location of my Python site-packages directory?

To find the location of my Python site-packages directory, we can call the site.getsitepackages method.

For instance, we write

import site

l = site.getsitepackages()

to call site.getsitepackages to return a list of package directory paths.

Conclusion

To find the location of my Python site-packages directory, we can call the site.getsitepackages method.

Categories
Python Answers

How to start a background process in Python?

Sometimes, we want to start a background process in Python.

In this article, we’ll look at how to start a background process in Python.

How to start a background process in Python?

To start a background process in Python, we call subprocess.Popen.

For instance, we write

import subprocess

subprocess.Popen(["rm","-r","some.file"])

to call subprocess.Popen with a list with the command and command arguments to run the command in the background.

Conclusion

To start a background process in Python, we call subprocess.Popen.

Categories
Python Answers

How to hide output of subprocess with Python?

Sometimes, we want to hide output of subprocess with Python.

In this article, we’ll look at how to hide output of subprocess with Python.

How to hide output of subprocess with Python?

To hide output of subprocess with Python, we can set stdout to subprocess.DEVNULL`.

For instance, we write

import os
import subprocess

c = subprocess.call(['echo', 'foo'], 
    stdout=subprocess.DEVNULL,
    stderr=subprocess.STDOUT)

to output the echo command’s output to dev null by setting the stdout to subprocess.DEVNULL when we call subprocess.call.

Conclusion

To hide output of subprocess with Python, we can set stdout to subprocess.DEVNULL`.