简体   繁体   English

如果文本文件中的行引用了不存在的文件,则将其删除

[英]Delete lines of text file if they reference a nonexistent file

I have a text file ( images1.txt ) with lists of .jpg names and I have a folder ( Bones ) with .jpg images. 我有一个包含.jpg名称列表的文本文件( images1.txt ),我还有一个包含.jpg图像的文件夹( Bones )。 All image names are exactly 42 characters (including the file extension), and each is on a separate line containing the name and some information about the image. 所有图像名称正好是42个字符(包括文件扩展名),并且每个字符都在包含该名称和有关图像的某些信息的单独行中。 For example: 例如:

OO75768249870G_2018051_4A284DQ0-011628.jpg,1A4502432KJL459265,emergency
OO75768249870G_2018051_4A284DQ0-011629.jpg,1A451743245122,appointment

where everything after .jpg is my own personal notes about the photos. .jpg之后的所有内容都是我对照片的个人注释。 Bones contains many of the 4,000+ images named in images1 but not all. Bones包含images1中命名的images1多个图像中的许多,但不是全部。 Using either the command prompt or python, how would I remove the lines from images1 which correspond to images not present in my Bones folder? 使用命令提示符或python,如何从images1删除与Bones文件夹中不存在的图像相对应的行?

Thanks! 谢谢!

In python: 在python中:

import os

LEN_OF_FILENAME = 42

with open('images1.txt', 'r') as image_file:
    with open('filtered_images1.txt', 'w') as filtered_image_file:
        for line in image_file:
            image_name = line[:LEN_OF_FILENAME]
            path_to_image = os.path.join('Bones', image_name)
            if os.path.exists(path_to_image):
                filtered_image_file.write(line)

Assuming images1.txt and Bones are in the same folder, if you run the above Python script in that folder you will get filtered_images1.txt . 假设images1.txtBones在同一个文件夹中,如果在该文件夹中运行上述Python脚本,您将得到filtered_images1.txt It will only contain lines that has a corresponding image in Bones . 它仅包含在Bones中具有相应图像的行。

This code will read the lines from image1.txt and create an image2.txt with the lines where the file exists in the bones directory. 这段代码将从image1.txt中读取各行,并使用骨骼目录中文件所在的行创建一个image2.txt。

@ECHO OFF
IF EXIST image2.txt (DEL image2.txt)
FOR /F "tokens=1,* delims=," %%f IN ('TYPE "image1.txt"') DO (
    IF EXIST "bones\%%~f" (ECHO %%f,%%g >>"image2.txt")
)
EXIT /B

I think the easiest way is to use the findstr command : 我认为最简单的方法是使用findstr命令

rem /* Search for lines in file `images1.txt` in a case-insensitive manner that literally begin
rem    with a file name found in the directory `Bones` which in turn matches the naming pattern;
rem    then write all matching lines into a temporary file: */
dir /B /A:-D "Bones\??????????????_???????_????????-??????.jpg" | findstr /LIBG:/ "images1.txt" > "images1.tmp"
rem // Overwrite original `images1.txt` file by the temporary file:
move /Y "images1.tmp" "images1.txt" > nul

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM