繁体   English   中英

如何检查我的文件夹中的文件是否与 my.csv 文件中指定的文件名一致?

[英]How do I check if files in my folder coincide with the file names specified in my .csv file?

我正在尝试构建一种方法来检查 my.csv 文件中的文件名是否与我的实际文件夹中的文件名匹配。 如果它们不匹配,我想删除 my.csv 文件上的整行。 这是我到目前为止所尝试的:

dir_path = Path('D:\audio_files')
    
csv_file_path = Path('D:\metadata.csv') 

lines = list()
files = list()

for f in os.listdir(dir_path):
    f = f.strip('.wav')
    files.append(str(f))

with open(csv_file_path, 'r') as read_file:
    reader = csv.reader(read_file)
    for row in reader:
        lines.append(row)
        for field in row:
            for f in files:
                if field != f:
                    print("Line Removed.")
                    lines.remove(row)

但是,我不断收到此错误:

Traceback (most recent call last):
File "file_checker.py", line 26, in <module>
lines.remove(row)
ValueError: list.remove(x): x not in list

我应该修复什么才能让它工作?

编辑:

这是 my.csv 文件的一个小样本。 这是非常直接的。 第一列包含不带扩展名的文件名,第二列包含文件名的标签。

名称 label
236421 Male_speech
124818 女声
426906 Male_speech

等等。

我基本上是在尝试将fname列中的名称与我的文件夹中的名称(扩展名为.wav )匹配,如果文件夹中不存在名称,则删除不存在的文件名的行。

编辑#2:

我设法通过一些本地帮助解决了这个问题。 这是最终产品:

dir_path = 'D:\audio'

csv_file_path = 'D:\original.csv'

#create a new file that contains the fnames on the cvs file that match the file names in my file folder
csv_new_file = open('D:\new.csv', 'w', newline="")

# create a writer variable that will allow me to write rows in my new csv file
csv_write = csv.writer(csv_new_file, delimiter=',', quotechar='"')

# "i" variable will allow me to write the headers from the original csv file
i = 0
with open(csv_file_path, 'r') as read_file:
    reader = csv.reader(read_file, delimiter=',', quotechar='"')
    for row in reader:
#If the row is the very first, the write it as is (headers)
        if i == 0:
            csv_write.writerow(row)  
            i += 1
            continue
#Check if the file path for my audio files with .wav extension exists and the write the row of the original csv in my new csv
        file_path = dir_path + '/' + row[0] + '.wav'
        if os.path.exists(file_path):
            csv_write.writerow(row)

#IMPORTANT to close files once finished!
csv_new_file.close()
read_file.close()

考虑这个块:

for f in files:
    if field != f:
        lines.remove(row)

这就是说如果 field 的值不等于 f 的值,请删除它。 好吧,如果 files 是文件列表,除非列表中的第一个元素与字段的值匹配,否则它将被删除,并且在该元素已被删除后迭代将继续。

相反,我建议将files设置为集合并检查集合中的成员资格

dir_path = Path('D:\audio_files')
    
csv_file_path = Path('D:\metadata.csv') 

lines = list()
files = set()

for f in os.listdir(dir_path):
    f = f.strip('.wav')
    files.add(str(f))

with open(csv_file_path, 'r') as read_file:
    reader = csv.reader(read_file)
    for row in reader:
        lines.append(row)
        for field in row:
            if field not in files:
                lines.remove(row)
                continue

我会亲自将这些循环拆分并构建行列表,然后遍历副本删除元素,但这可能只是个人喜好。

暂无
暂无

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

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