[Automation with python! ] Part 2: File operation

At the beginning

Hello, it is delicious. This article is the second implementation commentary article of Because I don't want to deal with people with dirty desktops. It becomes. This time it may be a little short. If you want to know about the implementation, please see the article!

File operation part

I used regular expressions and shutil here.

Ingenuity 1: Let's extract the capture time from the file name!

The genius of mac screenshots is that the file name has a capture time! Thanks to this, I no longer have to install something like exiftool. The prefix contains the prefix that is added to the head when screenshot. For more information here!

path="Screenshot 2020-10-22 10.28.04.png "
cvtpath = re.compile(
        prefix + r"[\s\S]*?(\d{4}).(\d\d).(\d\d)[\s\D]*?(\d\d?).(\d\d).(\d\d)")
flag = re.search(cvtpath, path)
if flag is not None and len(flag.groups()) == 6:
    date = flag.groups()
    date = list(map(lambda x: int(x), date))

Trouble 1: How to get the day of the week

If you think about it, how do you know that you are at a certain time on a certain day of the week?

Solution: You can get the day of the week with datetime. All you have to do is make something like UNIX time!

day = dtdt(*date).strftime("%a") #You can get the day of the week
# print(day)
# > Thu
dateemb = date[3] * 60 + date[4] #Now that we have one value, we can compare it.
# print(dateemb)
# 628

Trouble 2: There is no os.mv!

Solution: If you don't have an os, just rely on shutil!

import shutil
shutil.move(oldpath, newdir)

At the end

Thank you for reading. The previous article is here

Recommended Posts

[Automation with python! ] Part 2: File operation
[Automation with python! ] Part 1: Setting file
[Automation] Read mail (msg file) with Python
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
Solving Sudoku with Python (Part 2)
Scraping with Selenium + Python Part 2
Download csv file with python
Playing handwritten numbers with python Part 1
Extract the xz file with python
[Automation] Extract Outlook appointments with Python
[Python] Write to csv file 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
[Python] File operation using if statement
Excel aggregation with Python pandas Part 1
How to use Python with Jw_cad (Part 2 Command explanation and operation)
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
Play handwritten numbers with python Part 2 (identify)
FM modulation and demodulation with Python Part 3
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
FizzBuzz with Python3
Scraping with Python
Statistics with python
[python] vector operation
Scraping with Python
Python with Go
QGIS + Python Part 1
Twilio with Python
Play with 2016-Python
Python file processing
Tested with Python
Python OS operation
with syntax (Python)
Python: Scraping Part 1
Bingo with python
[Python] Matrix operation
Excel with Python