[Automation with python! ] Part 1: Setting file

At the beginning

Hello, it is delicious. This article is the first of the implementation commentary articles because I don't want to deal with people with dirty desktops. It becomes. If you want to know about the implementation, please see the article!

Creation and editing part of configuration file

This time I used yaml and csv for the config file.

Trouble 1: Where should the configuration file be?

It was good to go out so that the user could easily change the settings, but when I tried to make it cli, I was in trouble with the location. After all, I solved it by creating a folder called .myscreenshot under home dir. It may be a little force.

Trouble 2: "~" contained in path does not point to homedir

I wrote a configuration file and tried to move it happily, but it didn't work. When I wondered why, the "~" contained in the path was interpreted only as a nyoro.

dirname="~/.myscreenshot"

The solution is as follows

from os.path import expanduser
home=expanduser("~")
dirname=dirname.replace("~",home)

Ingenuity 1: Isn't it cool to set up a mac without permission? ?? ??

I implemented it by hitting a command using subprocess.

import subprocess
res = subprocess.run(
        "defaults read com.apple.screencapture location",
        shell=True,
        capture_output=True)
if res.returncode == 0:
    dirname = res.stdout[:-1].decode()
else:
    dirname = "~/Desktop"

res = subprocess.run(
    "defaults read com.apple.screencapture name",
    shell=True,
    capture_output=True)
if res.returncode == 0:
    prefix = res.stdout[:-1].decode()
else:
    prefix = "screenshot"

Since the return value also includes a line break, it is deleted. Also, since it is a byte type, it is decoded.

This is relatively easy to implement using a library called python-daemon.

The second part is here!

Recommended Posts

[Automation with python! ] Part 1: Setting file
[Automation with python! ] Part 2: File operation
[Automation] Read mail (msg file) with Python
Image processing with Python (Part 2)
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Scraping with Selenium + Python Part 1
Draw netCDF file with python
Studying Python with freeCodeCamp part2
Image processing with Python (Part 1)
Solving Sudoku with Python (Part 2)
Image processing with Python (Part 3)
UI Automation Part 2 in Python
Scraping with Selenium + Python Part 2
Download csv file with python
Playing handwritten numbers with python Part 1
[Automation] Extract Outlook appointments with Python
Implemented file download with Python + Bottle
Output to csv file with Python
[Automation] Send Outlook email with Python
Create an Excel file with Python3
Automate simple tasks with Python Part0
Excel aggregation with Python pandas Part 1
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Process Pubmed .xml data with python [Part 2]
Read CSV file with python (Download & parse CSV file)
Automate simple tasks with Python Part1 Scraping
Check the existence of the file with python
Quickly create an excel file with Python #python
100 Language Processing Knock with Python (Chapter 2, Part 2)
Let's read the RINEX file with Python ①
Working with Azure CosmosDB from Python Part.2
Excel aggregation with Python pandas Part 2 Variadic
100 Language Processing Knock with Python (Chapter 2, Part 1)
Create Excel file with Python + similarity matrix
FM modulation and demodulation with Python Part 2
Record with Python → Save file (sounddevice + wave)
[Part1] Scraping with Python → Organize to csv!
[Automation] Read a Word document with Python
I made a configuration file with Python
QGIS + Python Part 2
Script python file
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
QGIS + Python Part 1
Twilio with Python
Integrate with Python
Play with 2016-Python
Python file processing
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python: Scraping Part 1
Bingo with python