我有一个名为 ABC 的文件夹,其中有很多扩展名为 001.py、001.xls、001.pdf 等的文件。我想编写一个程序,在其中我们获取具有该文件名的列表
["C:\Users\Desktop\ABC\001.py", "C:\Users\Desktop\ABC\001.xls", "C:\Users\Desktop\ABC\001.pdf"]
我的代码:
import os
from os import listdir
from os.path import isfile, join
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path) #current path
cwd = os.getcwd()
list3 = []
onlyfiles = [f for f in listdir(cwd) if isfile(join(cwd, f))]
for i in onlyfiles:
list3.append(dir_path+"\\"+i)
print(list3)
我得到的输出为:
["C:\\Users\\Desktop\\ABC\\001.py", "C:\\Users\\Desktop\\ABC\\001.xls", "C:\\Users\\Desktop\\ABC\\001.pdf"]
我正在寻找输出:
["C:\Users\Desktop\ABC\001.py", "C:\Users\Desktop\ABC\001.xls", "C:\Users\Desktop\ABC\001.pdf"]
一只名叫tom的猫
相关分类