搜索扩展名为ts的文件失败

时间:2019-11-18 05:39:36

标签: python python-2.7

我试图找到带有ts扩展名的文件名,但是由于某种原因,它失败了。扩展名为ts的电影文件位于movie_path中。从XML,我只能得到不带扩展名的文件名。即使使用ts扩展名正确的文件名,也无法检查u'{c} .ts'.format(c = movie_id)是否不在movie_path:中。

import os, re, csv

files = {}

xml_path   = '/Users/roradhak/eVision/failed_assets/'
movie_path = os.listdir('/Users/roradhak/eVision/ts_check/')

movie_id = ""
movie_title = ""
content_id = ""
reason_1= ""
reason_2 = ""
xml_details = []
filecount =0

if os.path.exists("/Users/roradhak/eVision/xml_validate.csv"):
    os.remove("/Users/roradhak/eVision/xml_validate.csv")

for filename in os.listdir(xml_path):

    #if filename.endswith(".xml"):
        MOVIE_ERROR = "NO"
        TRAILER_ERROR = "NO"
        with open(xml_path + filename, "r") as f:
            filecount+=1
            print "Processed File ", filename, "Filecount:", filecount
            check_title=0
            for line in f:
                if "Content ID" in line:
                    content_id =re.search('.*>(.+)<',line)
                    movie_id = content_id.group(1)
                    print movie_id
                    if "CDATA" in movie_id:
                        content_id= re.search('.*<!*\[CDATA\[(.+)\]\]', movie_id)
                        movie_id = content_id.group(1)
                        print movie_id
                    print movie_id

                    if u'{c}.ts'.format(c=movie_id) not in movie_path:
                        MOVIE_ERROR = "YES"
                        reason_1 = "No Movie file"

                if "name title=\"Name\"" in line:
                    if check_title ==0:
                        title = re.search('.*>(.+)<',line)
                        movie_title = title.group(1)
                        check_title = 1
                        if "CDATA" in movie_title:
                            movie_title = re.search('.*<!*\[CDATA\[(.+)\]\]', movie_title)
                            movie_title = movie_title.group(1)

                if "\"Has_Trailer\">Y<" in line:
                    trailer_id = str(content_id.group(1))
                    trailer_id=trailer_id.replace('M','T',1)
                    print trailer_id
                    if u'{c}.ts'.format(c=trailer_id) not in movie_path:
                        TRAILER_ERROR = "YES"
                        reason_2 = "No Trailer file"
            if MOVIE_ERROR =="YES" or TRAILER_ERROR =="YES":
                with open("xml_validate.csv", mode='a') as file:
                    fieldnames = ['Movie ID', 'Movie Title', 'Comment1', 'Comment2']
                    writer = csv.DictWriter(file,fieldnames=fieldnames)

                    writer.writerow({'Movie ID': movie_id, 'Movie Title': movie_title,'Comment1':reason_1,'Comment2':reason_2})
        f.close()

1 个答案:

答案 0 :(得分:0)

我正在使用以下代码从python中的特定目录中搜​​索具有特定扩展名的文件:

import glob, os
os.chdir(os.getcwd())
for file in glob.glob("*.ts"):
print(file)

您可以将os.getcwd()替换为您要搜索的目录名称