代码很简单,用于遍历文件夹内文件的内容,返回的是文件夹的绝对路径。
这是文件夹下的内容:
下面直接上代码:
import os
def readFile(parent_path):
file_path_list = []
file_list=os.listdir(parent_path)
for file in file_list:
file_path = os.path.join(parent_path, file)
file_path_list.append(file_path)
print(file_path) # 打印文件路径
return file_path_list # 返回默认顺序的文件名称list
parent_dir = "C:/Users/Admin/Desktop/2/"
file_path_list = readFile(parent_dir)
#================================ 打印结果===================
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103831.340.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103837.339.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103843.338.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103849.339.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103855.336.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103901.337.bmp
C:/Users/Admin/Desktop/2/playback_20161127.mp4_20200522_103907.336.bmp
虽然很简单,但挺有用,所以写了下。