Sorting image files with Python (3)

Preface

Last time, I have finished generating the file of the deletion target list. The contents of the file look like this.

\TMP\200104\delete.txt


C:\TMP\IMG_2607(1).jpg
C:\TMP\IMG_2608(1).jpg

\TMP\200105\delete.txt


C:\TMP\IMG_2610(1).jpg
C:\TMP\IMG_2610(2).jpg
C:\TMP\IMG_2611(1).jpg

There is delete.txt in each year / month (yyyymm) folder that contains the full path of the file you want to delete line by line, so just read this and delete it. Since it is still in the testing stage, I will write the process for the test folder.

Write in Python 3

Python : 3.8.3

DeleteAll.py


import glob
import os

if __name__ == "__main__":
    files = glob.glob("C:\\tmp\\**\\delete.txt", recursive=True)

    for file in files:
        with open(file, "r") as f:
            for line in f.readlines():
                os.remove(line.strip())

Mr. glob. Why does'\ n'stick at the end when reading with readlines ()? (A little disturbing)

Write in C

C# : 8.0(Microsoft Visual Studio Community 2019)

DeleteAll.cs


using System;
using System.IO;
using System.Text;

namespace DeleteAll
{
    class DeleteAll
    {
        static void Main()
        {
            var files = Directory.GetFiles(@"c:\tmp", "delete.txt", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                foreach (var line in File.ReadAllLines(file, Encoding.GetEncoding("Shift-JIS")))
                {
                    File.Delete(line);
                }
            }
        }
    }
}

I don't usually think about it, but when I put it side by side with Python, the parentheses seemed to be very disturbing ...

Try writing in VB6 (using FileSystemObject)

VisualBasic : 6.0

DeleteAll.bas


Option Explicit

Private colFiles As Collection

Public Sub Main()
    Set colFiles = New Collection
    Call GetFiles("C:\TMP\")
    Call DeleteFiles(colFiles)
End Sub

Private Sub GetFiles(ByVal strSearchPath As String)
    Dim FSO As FileSystemObject

    Set FSO = New FileSystemObject

    Dim objSubFolder As Folder

    For Each objSubFolder In FSO.GetFolder(strSearchPath).SubFolders
        Call GetFiles(objSubFolder.Path)
    Next

    Dim objFile As file

    For Each objFile In FSO.GetFolder(strSearchPath).files
        If LCase(objFile.Name) = "delete.txt" Then
            Call colFiles.Add(FSO.BuildPath(strSearchPath, objFile.Name))
        End If
    Next
End Sub

Private Sub DeleteFiles(ByVal files As Collection)
    Dim FSO As FileSystemObject
    Dim vntFile As Variant

    Set FSO = New FileSystemObject

    For Each vntFile In files
        With FSO.GetFile(vntFile).OpenAsTextStream(ForReading)
            Do
                Kill .ReadLine()
            Loop Until (.AtEndOfLine())
        End With
    Next
End Sub

In the old-fashioned Hungarian notation. It is very troublesome to implement recursive search by yourself. With this, it is important to bind without using FileSystemObject. The variable declaration in GetFiles () is written to declare Dim just before it appears, but in the old VB6 source, it is customary to write the declaration collectively at the beginning of the method. I think it's the style of people who flowed C-> VB, but I often see shit code that has 500 lines per method only for sources written like that.

Try writing in a batch file

DeleteAll.bat


@echo off
cd \tmp
for /f "usebackq" %%i in (`dir /s /b delete.txt`) do (for /f %%j in (%%i) do del %%j)

How simple a batch file is! Instead, the format is a bit esoteric.

Afterword

This completes this case. Now, what are we going to do next?

Recommended Posts

Sorting image files with Python (2)
Sorting image files with Python (3)
Sorting image files with Python
Image processing with Python
Image processing with Python (Part 2)
Image editing with python OpenCV
Sort huge files with python
Image processing with Python (Part 1)
Tweet with image in Python
Integrate PDF files with Python
Image processing with Python (Part 3)
Reading .txt files with Python
[Python] Image processing with scikit-image
Extract the table of image files with OneDrive & Python
Cut out an image with python
[Python] Using OpenCV with Python (Image Filtering)
Recursively unzip zip files with python
Manipulating EAGLE .brd files with Python
Decrypt files encrypted with OpenSSL with Python 3
Image processing with Python 100 knocks # 3 Binarization
Let's do image scraping with Python
Handle Excel CSV files with Python
Read files in parallel with Python
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Send image with python, save with php
Sorting files by Python naming convention
Gradation image generation with Python [1] | np.linspace
[Python] Easy reading of serial number image files with OpenCV
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
[AWS] Using ini files with Lambda [Python]
Play audio files from Python with interrupts
python image processing
HTML email with image to send with python
Statistics with python
Create a dummy image with Python + PIL.
Python with Go
Image processing with Python 100 knocks # 8 Max pooling
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Twilio with Python
Integrate with Python
Play with 2016-Python
Decrypt files encrypted with openssl from python with openssl
AES256 with python
Use cryptography library cryptography with Docker Python image
Image processing with Python & OpenCV [Tone Curve]
Tested with Python
Image processing with Python 100 knock # 12 motion filter
Algorithm learned with Python 19th: Sorting (heapsort)
python starts with ()
Image acquisition from camera with Python + OpenCV
[Python] I made an image viewer with a simple sorting function.
Reading and writing JSON files with Python
Download files on the web with Python
[Easy Python] Reading Excel files with openpyxl
with syntax (Python)
Bingo with python
Drawing with Matrix-Reinventor of Python Image Processing-
Zundokokiyoshi with python
Easy image processing in Python with Pillow