为什么 python 操作系统库在一个文件上返回错误,而在其他三个文件上则不返回错误?

问题描述 投票:0回答:1

收集文件名和大小的简单脚本在第四个文件上失败,并出现以下错误:

“FileNotFoundError:[WinError 2]系统找不到指定的文件:'A023_04.mp3'”

import os

siteID = 'A023' #This must match audio and NVSPL directories and filenames
drive_audio = "E"
dirName_audio = drive_audio + ':\\data\\audio\\' + siteID + '\\'

fileNames_MP3 = [file for file in os.listdir(dirName_audio)] 

# this works:
for val in fileNames_MP3:
    print(val)
print('\n')
    
# this works for the first three files:
for val in fileNames_MP3:
    file_stats=os.stat(val)
    print(val, file_stats.st_size)
    

这是输出:

A023_01.mp3
A023_02.mp3
A023_03.mp3
A023_04.mp3


A023_01.mp3 1073672486
A023_02.mp3 1073644272
A023_03.mp3 1073644273
Traceback (most recent call last):
  File "E:\data\mp3_garage\funWithFileInfo.py", line 16, in <module>
    file_stats=os.stat(val)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'A023_04.mp3'

我可以使用 Windows 文件管理器查看属性,并且与其他文件相比,没有发现该特定文件有任何奇怪之处。

python operating-system
1个回答
0
投票

我要做的第一件事是打印出每个 val 的长度以及值本身(在第一个循环中):

print(len(val), val)

这是因为有趣的是,两个输出块之间有 
two

空行。

可能

是您的转录错误,或者可能是文件名中嵌入了奇怪的字符。

© www.soinside.com 2019 - 2024. All rights reserved.