使用Python在目录中大容量追加文件名
我刚刚开始使用Python,这是我第一个“自己”的程序。我已经(尽我所能)解释了我计划实现的目标。因为我是新来的,所以如果您有任何语法/性能改进建议,我将不胜感激使用Python在目录中大容量追加文件名,python,json,regex,python-3.x,url,Python,Json,Regex,Python 3.x,Url,我刚刚开始使用Python,这是我第一个“自己”的程序。我已经(尽我所能)解释了我计划实现的目标。因为我是新来的,所以如果您有任何语法/性能改进建议,我将不胜感激 ''' Search through a target movies directory and filter movie titles to search IMDb for movie ratings. After fetching, append ratings to corr
'''
Search through a target movies directory and filter movie titles
to search IMDb for movie ratings. After fetching, append ratings
to corresponding movie files/folders in the directory.
File names are in one of the following formats:
1. P.S. I Love You.mkv
2. P.S. I Love You (2010).mp4
3. P.S. I Love You (2010) [1080p].avi
Ideally, this program fetched the movie ratings and adds it to
the end of the file name (just before the extension). The query,
in this case, would be http://www.omdbapi.com/?t=P.S.+I+Love+You
Ideally, the file in the directory would be renamed to one of the
following:
1. P.S. I Love You (7.1).mkv
2. P.S. I Love You (2010) (7.1).mp4
3. P.S. I Love You (2010) [1080p] (7.1).avi
'''
import os, json, urllib.request, re
# Query related constants
base_uri = "http://www.omdbapi.com/?"
query_title = "t="
basepath = "E:/Movies"
# Fetch movie rating from omdbapi.com
# Example JSON response: http://www.omdbapi.com/?t=insurgent
def getRating(movie_title):
# json_response = urllib.urlopen(base_uri + query_title + movie_title)
# movie_data = json.loads(json_response.read())
with urllib.request.urlopen(base_uri + query_title + movie_title) as url:
movie_data = url.read()
return movie_data['imdbRating']
# Checks if parameter file name already has a rating.
# Movie ratings are
def hasRating(filename):
pattern = re.compile('\([0-9].[0-9]\)')
if pattern.search(filename) is not None:
return True
return False
# Get the movie title by stripping out excess information such as the
# year released or video definition
def getMovieTitle(filename):
if '(' not in filename is False:
return filename.split('(')[0]
elif'[' not in filename is False:
return filename.split('[')[0]
return os.path.splitext(basepath + filename)[:-1]
def main():
for file in os.listdir(basepath):
if hasRating(file) is False:
movie_title = getMovieTitle(file)
file_ext = os.path.splitext(basepath + file)[-1:]
movie_rating = getRating(movie_title)
formatted_rating = ' (' + movie_rating + ')'
file_no_ext = os.path.splitext(basepath + file)[:-1]
os.rename(file, file_no_ext + ' ' + formatted_rating + file_ext)
if __name__ == '__main__':
main()
到目前为止,我已尝试修复所有我能修复的问题,但我始终遇到相同的错误:
Traceback (most recent call last):
File "renamer.py", line 65, in <module>
main()
File "renamer.py", line 58, in main
movie_rating = getRating(movie_title)
File "renamer.py", line 33, in getRating
with urllib.request.urlopen(base_uri + query_title + movie_title) as url:
TypeError: Can't convert 'tuple' object to str implicitly
回溯(最近一次呼叫最后一次):
文件“renamer.py”,第65行,在
main()
文件“renamer.py”,第58行,主
电影分级=获取分级(电影标题)
getRating中第33行的文件“renamer.py”
使用urllib.request.urlopen(基本uri+查询标题+电影标题)作为url:
TypeError:无法将“tuple”对象隐式转换为str
请告诉我什么是TypeError
,为什么会发生,以及我能做些什么来修复它
Python源于Java,它的简单性让人难以抗拒,但同时也让人耳目一新。无论如何,提前感谢您的输入 在您的情况下,
getMovieTitle
如果条件为true,则返回一个元组。因此,movie\u title
是一个元组,getRating
无法处理它(“添加”字符串和元组会导致TypeError
异常)
返回列表的最后一个元素由[-1]
完成。您希望从os.path.splitext中使用的是第一部分,所以使用索引0
return os.path.splitext(basepath + filename)[:-1]
把这条线转到
return os.path.splitext(basepath + filename)[0]
其次,通过movie\u data['imdbRating']
读取url.read()
后,您将访问movie\u数据
,这是一个字符串。您最可能想要的是解码结果(即json),然后访问元素:
with urllib.request.urlopen(base_uri + query_title + movie_title) as url:
movie_data = json.loads(url.read())['imdbRating']
只要“imdbRating”是根字典中的一个键,它就可以工作。在本例中,
getMovieTitle
如果条件为true,则返回一个元组。因此,movie\u title
是一个元组,getRating
无法处理它(“添加”字符串和元组会导致TypeError
异常)
返回列表的最后一个元素由[-1]
完成。您希望从os.path.splitext中使用的是第一部分,所以使用索引0
return os.path.splitext(basepath + filename)[:-1]
把这条线转到
return os.path.splitext(basepath + filename)[0]
其次,通过movie\u data['imdbRating']
读取url.read()
后,您将访问movie\u数据
,这是一个字符串。您最可能想要的是解码结果(即json),然后访问元素:
with urllib.request.urlopen(base_uri + query_title + movie_title) as url:
movie_data = json.loads(url.read())['imdbRating']
只要“imdbRating”是根字典中的一个键,它就可以工作。在该语句中,我试图返回不带扩展名的文件名。因此,我返回除了文件名的最后一部分(扩展名)以外的所有内容。然后使用
'.join(os.path.splitext(basepath+filename)[:-1])
,它通过一个空字符串连接元组的值('a','b','c')
获取'abc'
splitext
给出一个包含两个值的元组:(root,ext)
。使用索引0使其按预期工作我认为它返回了一个字符串列表,按(可能是默认值)
字符进行拆分。不,它分隔了最后一个扩展名。请参见splitext
上的os.path.splitext('test.doc.exe')
返回('test.doc','exe')
。在该语句中,我试图返回不带扩展名的文件名。因此,我返回除了文件名的最后一部分(扩展名)以外的所有内容。然后使用'.join(os.path.splitext(basepath+filename)[:-1])
,它通过一个空字符串连接元组的值('a','b','c')
获取'abc'
splitext
给出一个包含两个值的元组:(root,ext)
。使用索引0使其按预期工作我认为它返回了一个字符串列表,按(可能是默认值)
字符进行拆分。不,它分隔了最后一个扩展名。请参见splitext
上的os.path.splitext('test.doc.exe')
返回('test.doc','exe')
。