[英]How to match the .mp4 files present in a folder with the names in .csv file and sort according to some column value, in python?
I have a folder containing about 500.mp4 files:我有一个包含大约 500.mp4 文件的文件夹:
abc.mp4
lmn.mp4
ijk.mp4
Also I have a.csv file containing the file names (>500) and some values associated with them:我还有一个.csv 文件,其中包含文件名 (>500) 和一些与它们相关的值:
file name value
abc.mp4 5
xyz.mp4 3
lmn.mp4 5
rgb.mp4 4
I want to match the file names of.csv and folder and then place the mp4 files in separate folders depending on the value.我想匹配 .csv 和文件夹的文件名,然后根据值将 mp4 文件放在单独的文件夹中。
**folder 5:**
abc.mp4
lmn.mp4
**folder 3:**
xyz.mp4
and so on
names=[]
names1=[]
for dirname, dirnames, filenames in os.walk('./videos_test'):
for filename in filenames:
if filename.endswith('.mp4'):
names.append(filename)
file = open('names.csv',encoding='utf-8-sig')
lns = csv.reader(file)
for line in lns:
nam = line [0]
sc=line[1]
names1.append(nam)
if nam in names:
print (nam, line[1])
if line[1]==5
print ('5')
print(nam) %just prints the name of file not save
else if line[1]==3
print ('3')
print(nam)
does not give any result.没有给出任何结果。
I'd recommend you to use pandas
if you're going to handle csv
files.如果您要处理
csv
文件,我建议您使用pandas
。
Here's a code that will automatically create the folders, and put the files in the right place for you using shutil
and pandas
.这是一个自动创建文件夹的代码,并使用
shutil
和pandas
将文件放在正确的位置。 I have assumed that your csv's columns are "filename" and "value".我假设您的 csv 列是“文件名”和“值”。 Change them if there's a mismatch.
如果不匹配,请更改它们。
import pandas as pd
import shutil
import os
path_to_csv_file = "file.csv"
df = pd.read_csv(path_to_csv_file)
mp4_root = "mp4_root"
destination_path = "destination_path"
#In order to remove the folder if previously created. You can delete this if you don't like it.
if os.path.isdir(destination_path):
shutil.rmtree(destination_path)
os.mkdir(destination_path)
unique_values = pd.unique(df['value'])
for u in unique_values:
os.mkdir(os.path.join(destination_path, str(u)))
#Here we iterate over the rows of your csv file, and concatenate the value and the filename to the destination_path with our new folder structure.
for index, row in df.iterrows():
cur_path = os.path.join(destination_path, str(row['value']), str(row['filename']))
source_path = os.path.join(mp4_root, str(row['filename']))
shutil.copyfile(source_path, cur_path)
EDIT : If there's a file that is in the csv but not present in the source folder, you could check it before (more pythonic) or you could handle it via a try/catch exception check.(Not recommended)编辑:如果 csv 中有一个文件但不存在于源文件夹中,您可以之前检查它(更多 pythonic)或者您可以通过 try/catch 异常检查来处理它。(不推荐)
Check the code below.检查下面的代码。
source_files = os.listdir(mp4_root)
for index, row in df.iterrows():
if str(row['filename']) not in source_files:
continue
cur_path = os.path.join(destination_path, str(row['value']), str(row['filename']))
source_path = os.path.join(mp4_root, str(row['filename']))
shutil.copyfile(source_path, cur_path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.