CLI that find the Pictures folder of the operating system

Viewed 95

How can I get this program to find the Pictures folder on any operating system? E.g. Windows, Linux, and Mac.

My program will just take an input and create a QRcode image.

I know there are some standard modules to help me, like os and sys, but I wasn't able to figure it out on my own.

import qrcode

data = 'This is an example.'
qr = qrcode.QRCode(version=1)
qr.add_data(data)
img = qr.make_image(fill_color='white', back_color='purple')
img.save('$HOME/Pictures/qrcode_make_python.png')
1 Answers

You can use pathlib.Path.home() for that.

Here is an example:

from qrcode import QRCode
from pathlib import Path

pictures_folder = Path.home() / 'Pictures'

if pictures_folder.is_dir():
    data = 'https://stackoverflow.com'
    qr = QRCode(version=1)
    qr.add_data(data)
    img = qr.make_image(fill_color='white', back_color='purple')

    img.save(pictures_folder / 'qrcode.png')
else:
    print(f"{pictures_folder} folder doesn't exist!")

The snippet above is portable and will work on most platforms supported by Python.

Related