Python3 批量重命名
Python3 batch rename
我是 python 的新手,正在尝试制作脚本,该脚本按此模式重命名目录中的所有文件 ShowName + SeasonNumber + EpisodeNumber
此脚本获取文件名 argv[1]
和目录 argv[2]
。然后在 find_needed_parts
中解析字符串和 returns 季数、剧集和节目名称。但是这个脚本正在删除这样的回溯:
Traceback (most recent call last):
File "/home/usrname/Documents/renameFiles/main.py", line 38, in <module>
main()
File "/home/usrname/Documents/renameFiles/main.py", line 35, in main
os.rename(pathAndFilename, os.path.join(dir, rename_name))
File "/usr/lib/python3.5/posixpath.py", line 89, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib/python3.5/genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'builtin_function_or_method'
这是代码:
#!/usr/bin/python3
import glob
import os
import sys
import re
def find_needed_parts(filename):
ep_reg = re.search("(E|e)(\d+)", filename)
if ep_reg:
found_ep = ep_reg.group(2)
s_reg = re.search("(S|s)(\d+)", filename)
if s_reg:
found_s = s_reg.group(2)
ext_reg = re.search("\.(\w+)", filename)
if ext_reg:
ext = ext_reg.group(1)
body_reg = re.search("((^(.*?)([^E0-9])+)|(^(.*?)([^e0-9])))", filename)
if body_reg:
body = body_reg.group(1)
return body, found_ep, found_s, ext
def main():
filename = sys.argv[1]
direct = sys.argv[2]
body, ep, s, ext = find_needed_parts(filename)
pattern = "*." + ext
for pathAndFilename in glob.iglob(os.path.join(direct, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
ep = int(ep) + 1 # type: int
rename_name = str(body + 'S' + s + 'E' + str(ep) + '.' + ext)
os.rename(pathAndFilename, os.path.join(dir, rename_name))
main()
UPD1。
这是参数
argv[1] = abcd E01 S09 ockoeko ko k.avi
argv[2] = .
主要问题在语句
os.rename(pathAndFilename, os.path.join(dir, rename_name))
你在哪里使用 dir
built-in function,而你可能是指
os.rename(pathAndFilename, os.path.join(direct, rename_name))
我是 python 的新手,正在尝试制作脚本,该脚本按此模式重命名目录中的所有文件 ShowName + SeasonNumber + EpisodeNumber
此脚本获取文件名 argv[1]
和目录 argv[2]
。然后在 find_needed_parts
中解析字符串和 returns 季数、剧集和节目名称。但是这个脚本正在删除这样的回溯:
Traceback (most recent call last):
File "/home/usrname/Documents/renameFiles/main.py", line 38, in <module>
main()
File "/home/usrname/Documents/renameFiles/main.py", line 35, in main
os.rename(pathAndFilename, os.path.join(dir, rename_name))
File "/usr/lib/python3.5/posixpath.py", line 89, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib/python3.5/genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'builtin_function_or_method'
这是代码:
#!/usr/bin/python3
import glob
import os
import sys
import re
def find_needed_parts(filename):
ep_reg = re.search("(E|e)(\d+)", filename)
if ep_reg:
found_ep = ep_reg.group(2)
s_reg = re.search("(S|s)(\d+)", filename)
if s_reg:
found_s = s_reg.group(2)
ext_reg = re.search("\.(\w+)", filename)
if ext_reg:
ext = ext_reg.group(1)
body_reg = re.search("((^(.*?)([^E0-9])+)|(^(.*?)([^e0-9])))", filename)
if body_reg:
body = body_reg.group(1)
return body, found_ep, found_s, ext
def main():
filename = sys.argv[1]
direct = sys.argv[2]
body, ep, s, ext = find_needed_parts(filename)
pattern = "*." + ext
for pathAndFilename in glob.iglob(os.path.join(direct, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
ep = int(ep) + 1 # type: int
rename_name = str(body + 'S' + s + 'E' + str(ep) + '.' + ext)
os.rename(pathAndFilename, os.path.join(dir, rename_name))
main()
UPD1。 这是参数
argv[1] = abcd E01 S09 ockoeko ko k.avi
argv[2] = .
主要问题在语句
os.rename(pathAndFilename, os.path.join(dir, rename_name))
你在哪里使用 dir
built-in function,而你可能是指
os.rename(pathAndFilename, os.path.join(direct, rename_name))