Home > Software engineering >  CLI that find the Pictures folder of the operating system
CLI that find the Pictures folder of the operating system

Time:06-27

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')

CodePudding user response:

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.

CodePudding user response:

first you should check the operating system, i suggest using sys.platform

import sys
if sys.platform == 'win32':
    print("You are in windows")
elif sys.platform == 'darwin':
    print("You are in MacOS")
elif sys.platform == 'linux' or sys.platform=='linux2':
    print("You are in Linux')

to get the user path in windows we'll use os.expanduser("~"), that will get us the path to insert.

for getting the directory we'll use the setup:

import sys
import os
if sys.platform=='linux' or sys.platform=='linux2':
    img.save("~/Pictures/qrcode_make_python.png")
elif sys.platform == 'win32':
    userpath = os.expanduser("~")
    img.save(f"{userpath}\\Pictures\\qrcode_make_python.png")
elif sys.platform == 'darwin':
    img.save("${HOME}/Pictures/qrcode_make_python.png")
else:
    # Use file stderr if you want to communicate an error
    print("ERROR: unrecognized operating system!",file=sys.stderr)
    # or you could do 
    # sys.stderr.write("ERROR: unrecognized operating system!")

or we could use the windows api to get the information directly:

import sys
import win32com.shell as shell
import win32com.shell.win32con as con
if sys.platform=='linux' or sys.platform=='linux2':
    img.save("~/Pictures/qrcode_make_python.png")
elif sys.platform == 'win32':
    picturesfolder = shell.SHGetSpecialFolder(0,con.CSIDL_MYPICTURES)
    img.save(f"{picturesfolder}\\qrcode_make_python.png")
elif sys.platform == 'darwin':
    img.save("${HOME}/Pictures/qrcode_make_python.png")
else:
    # Use file stderr if you want to communicate an error
    print("ERROR: unrecognized operating system!",file=sys.stderr)

this isn't bad but if we want to make a solution that will fit almost every OS and in less space we can use pathlib.Path.expanduser() or pathlib.Path.home() to get the home directory of the current user.

then what we can do is add that to 'Pictures' and it's done, here is an example of how we could accomplish that:

import pathlib
to_save = pathlib.Path.home()/'Pictures'/'qrcode_make_python.png'
if to_save.exists():
    img.save(to_save)
else:
    print("WARNING: Default path is not avaiable, downloading in same directory.")
    img.save("qrcode_make_python.png")
print("Image Saved!")

CodePudding user response:

Ok, XxJames- gave the best answer. I researched it a little more and consolidated it to be more pythonic. I hope this will help other people as well.

Warning!!! I did not test on Windows or Mac, Linux works though.

if sys.platform.startswith("linux"):
    print("You are in Linux")
    userpath = os.path.expanduser("~")
    img.save(f"{userpath}/Pictures/{file_name}")
elif sys.platform.startswith("win"):
    print("You are in windows")
    userpath = os.path.expanduser("~")
    img.save(f"{userpath}/Pictures/{file_name}")
elif sys.platform.startswith("darwin"):
    print("You are in MacOS")
    userpath = os.path.expanduser("~")
    img.save(f"{userpath}/Pictures/{file_name}")
    #img.save(f"${HOME}/Pictures/{file_name}")
  • Related