Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python操作系统模块找不到文件_Python - Fatal编程技术网

Python操作系统模块找不到文件

Python操作系统模块找不到文件,python,Python,我在与python文件相同的目录中有两个文件夹,mrrobot和subs。我有四个文件,两个文件中有四个不同的名称。我将文件名存储在mr robot中,列表中没有扩展名(文件)。我想用列表中的名称重命名subs目录中的文件。但当我运行它时,它会抛出以下错误: FileNotFoundError: [WinError 2] The system cannot find the file specified 这是我的密码: import os currdir = os.getcwd() vidf

我在与python文件相同的目录中有两个文件夹,
mrrobot
subs
。我有四个文件,两个文件中有四个不同的名称。我将文件名存储在
mr robot
中,列表中没有扩展名(
文件
)。我想用列表中的名称重命名
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')

我认为问题在于,您没有为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')

我对您的代码进行了一些修改,以显示发生了什么:

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
但是,由于没有从视频文件到字幕文件的映射,您很可能会以错误的顺序结束,因为即使按字母顺序排序会导致文件匹配:

返回包含目录中条目名称的列表 由路径给出该列表按任意顺序排列,不包括 特殊条目“.”和“…”即使它们出现在 目录

所以你应该自己分类:

  • (到位)
  • (新名单)

此外,另一个答案是正确的,因为您还需要提供文件路径(或更改工作目录),例如:


您将进行4*4重命名-这可能不是您想要的。。此外,请在将来发布完整的错误消息,或编辑您的问题以包含它。更好的是,提供您的测试文件,以便这里的人可以运行您的代码(另请参见)。它实际给出了错误的哪一行?确切的代码在我的系统上工作。
os.rename("subs/"+sub, "subs/"+newname)