与python文件mr robot和subs在同一个目录中有两个文件夹。我有四个文件,在两个文件中都有四个不同的名字。我将文件名存储在mr robot中,列表中没有扩展名(file_s)。我想用列表中的名称重命名subs目录中的文件。但当我运行它时,它会抛出以下错误:
FileNotFoundError: [WinError 2] The system cannot find the file specified这是我的代码:
import os
currdir = os.getcwd()
vidfiles = os.listdir(f'{currdir}/mr robot')
subfiles = os.listdir(f'{currdir}/subs')
file_s = []
for file in vidfiles:
filename, _ = os.path.splitext(file)
file_s.append(filename)
for filename in file_s:
for sub in subfiles:
os.rename(sub, f'{filename}.srt')发布于 2019-10-22 09:29:31
我认为问题在于,您没有将子文件的全部路径提供给os.rename。
此代码适用于我:
import os
currdir = os.getcwd()
mr_robotpath = f'{currdir}/mr robot'
subspath = f'{currdir}/subs'
vidfiles = os.listdir(mr_robotpath)
subfiles = os.listdir(subspath)
file_s = []
for file in vidfiles:
filename, _ = os.path.splitext(file)
file_s.append(filename)
for sub,filename in zip(subfiles,file_s):
os.rename(f"{subspath}/{sub}", f'{subspath}/{filename}.srt')发布于 2019-10-22 09:37:15
我对您的代码做了一些修改,以显示正在发生的事情:
import os
# setup
print("trying to create some files")
try:
os.mkdir("mr robot")
os.mkdir("subs")
for f in "one two three four".split():
print(f)
with open("mr robot/"+f+".vid", "w") as h:
pass
with open("subs/"+f+".sth", "w") as h:
pass
except FileExistsError as e:
print("already there")
print("(Error: {})".format(e))
currdir = os.getcwd()
vidfiles = os.listdir(f'{currdir}/mr robot')
subfiles = os.listdir(f'{currdir}/subs')
file_s = []
#debug
print("vidfiles", vidfiles)
print("subfiles", subfiles)
for file in vidfiles:
filename, _ = os.path.splitext(file)
file_s.append(filename)
print("file_s", file_s)
for filename in file_s:
print("filename", filename)
for sub in subfiles:
newname = f'{filename}.srt'
print(f" {sub} -> {newname}")
# os.rename(sub, newname)它的产出如下:
trying to create some files
already there
(Error: [WinError 183] Cannot create a file when that file already exists: 'mr robot')
vidfiles ['four.vid', 'one.vid', 'three.vid', 'two.vid']
subfiles ['four.sth', 'one.sth', 'three.sth', 'two.sth']
file_s ['four', 'one', 'three', 'two']
filename four
four.sth -> four.srt
one.sth -> four.srt
three.sth -> four.srt
two.sth -> four.srt
filename one
four.sth -> one.srt
one.sth -> one.srt
three.sth -> one.srt
two.sth -> one.srt
filename three
four.sth -> three.srt
one.sth -> three.srt
three.sth -> three.srt
two.sth -> three.srt
filename two
four.sth -> two.srt
one.sth -> two.srt
three.sth -> two.srt
two.sth -> two.srt正如所怀疑的(请参阅注释),您有两个循环,结果是4*4重命名。
修改后的代码更接近于您试图实现的目标:
import os
currdir = os.getcwd()
vidfiles = os.listdir(f'{currdir}/mr robot')
subfiles = os.listdir(f'{currdir}/subs')
file_s = []
# debug
print("vidfiles", vidfiles)
print("subfiles", subfiles)
for file in vidfiles:
filename, _ = os.path.splitext(file)
file_s.append(filename)
print("file_s", file_s)
for i,sub in enumerate(subfiles):
newname = f'{file_s[i]}.srt'
print(f" {sub} -> {newname}")
# os.rename(sub, newname)印出来
vidfiles ['four.vid', 'one.vid', 'three.vid', 'two.vid']
subfiles ['four.sth', 'one.sth', 'three.sth', 'two.sth']
file_s ['four', 'one', 'three', 'two']
four.sth -> four.srt
one.sth -> one.srt
three.sth -> three.srt
two.sth -> two.srt但是,由于不存在从视频文件到字幕文件的映射,您可能会得到错误的顺序,因为即使按字母顺序排序会导致文件匹配:
https://docs.python.org/3/library/os.html#os.listdir
返回一个列表,其中包含路径给出的目录中条目的名称。列表是按任意顺序排列的,不包括特殊条目'.‘。还有“..”即使它们存在于目录中。
所以你应该自己整理一下:
)
此外,另一个答案是正确的,因为您还需要提供文件的路径(或更改工作目录),例如:
os.rename("subs/"+sub, "subs/"+newname)https://stackoverflow.com/questions/58500679
复制相似问题