[英]os.walk in Python
我试图遍历音频文件的目录,以便将其数据打印到Excel工作表中。 现在,我只能访问根文件夹中的文件-我被困于访问任何子文件夹中的文件。 当我在'if os.path.isfile(audio_file):'行之前'print(audio_file)'时,它循环遍历所有目录中的所有文件,但是我有'isfile'行,因此下面的代码仅查看文件。 我被困在这一点上,不知道该怎么办。
os.walk循环位于代码的底部:
import openpyxl, os, wave, time, datetime
from openpyxl.styles import Font
from datetime import datetime
os.chdir('c:\\Users\\Sonorityscape\\Desktop\\audiofiles')
wb = openpyxl.Workbook()
sheet = wb['Sheet']
headings = {'A1' : 'File Name',
'B1' : 'Relative Path',
'C1' : 'File Size',
'D1' : 'Date of Creation',
'E1' : 'File Length (hr:mn:sc.msc)',
'F1' : 'Channels',
'G1' : 'Sample Rate',
'H1' : 'Bit Depth'}
cell_widths = {'A' : 30,
'B' : 40,
'C' : 11.5,
'D' : 19,
'E' : 25.75,
'F' : 10.5,
'G' : 13.5,
'H' : 10.5}
for k, v in headings.items():
sheet[k].value = v
sheet[k].font = Font(bold = True, name = 'Calibri', size = 12)
for k, v in cell_widths.items():
sheet.column_dimensions[k].width = float(v)
def file_info(file_name, folder_name, file_size, date_time, col_file, col_folder, col_size, col_time):
sheet.cell(row = r, column = col_file).value = file_name
sheet.cell(row = r, column = col_folder).value = folder_name
sheet.cell(row = r, column = col_size).value = str(round((file_size / 2**20), 3)) + ' MB'
sheet.cell(row = r, column = col_time).value = date_time
def sample_rate(frames, col):
frame_rate = w.getframerate()
file_length = (frames / frame_rate)
length_milliseconds = str(round((frames / frame_rate), 3))
sheet.cell(row = r, column = col).value = str(time.strftime('%H:%M:%S', time.gmtime(file_length))) + '.' + length_milliseconds[-3:]
def wave_info(channels, frame_rate, bit_depth, col_chan, col_fr, col_bit):
sheet.cell(row = r, column = col_chan).value = channels
sheet.cell(row = r, column = col_fr).value = str(frame_rate) + ' Hz'
sheet.cell(row = r, column = col_bit).value = str(bit_depth * 8) + ' bit'
r = 3
for root, dirs, files in os.walk('c:\\Users\\Sonorityscape\\Desktop\\audiofiles'):
for audio_file in files:
if os.path.isfile(audio_file):
w = wave.open(audio_file)
file_name = os.path.basename(audio_file)
folder_name = os.path.relpath('c:\\audiofiles\\' + audio_file, 'c:\\desktop')
file_size = os.path.getsize(audio_file)
time_created = os.path.getctime(audio_file)
date_time = datetime.fromtimestamp(time_created).strftime('%b. %d, %Y, %H:%M:%S')
frames = w.getnframes()
if w.getnchannels() == 2:
channels = 'Stereo'
else:
channels = 'Mono'
frame_rate = w.getframerate()
bit_depth = w.getsampwidth()
file_info(file_name, folder_name, file_size, date_time, 1, 2, 3, 4)
sample_rate(frames, 5)
wave_info(channels, frame_rate, bit_depth, 6, 7, 8)
w.close()
r += 1
wb.save('c:\\users\\sonorityscape\\desktop\\audiofilesexcel.xlsx')
您需要修改以下行:
if os.path.isfile(audio_file):
至
if os.path.isfile(os.path.join(root,audio_file)):
由于audio_file只是文件名的字符串,因此应将abs路径添加到文件中。 您使用代码更改了当前路径:
os.chdir('c:\\Users\\Sonorityscape\\Desktop\\audiofiles')
这就是为什么您可以在根目录中获得文件,但不能在子文件夹中获得文件的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.