代码不知道哪出错了 求助
原是想把目录下所有txt文件路径放到一个列表内----------------------------------------------------------------------------------------------------------------------------------
import os
def find_txt(address):
list1 = []
os.chdir(address)
for item in os.listdir(os.curdir):
if os.path.isdir(item):
find_txt(item)
os.chdir(os.pardir)
if os.path.splitext(item) == '.txt':
list1.append(os.getcwd() + os.sep + item)
print(list1)
address = 'C:/Users/34292/Desktop/abc'
find_txt(address)
-------------------------------------------------------------------------------------------------------------------------------------
下面是结果
-------------------------------------------------------------------------------------------------------------------------------------
['C:\\Users\\34292\\Desktop\\abc\\C\\111.txt', 'C:\\Users\\34292\\Desktop\\abc\\C\\666.txt']
['C:\\Users\\34292\\Desktop\\abc\\D\\H\\222.txt', 'C:\\Users\\34292\\Desktop\\abc\\D\\H\\555.txt']
[]
['C:\\Users\\34292\\Desktop\\abc\\E\\O\\444.txt']
[]
['C:\\Users\\34292\\Desktop\\abc\\E\\333.txt', 'C:\\Users\\34292\\Desktop\\abc\\E\\777.txt']
[] import os
def find_txt(address):
list1 = []
os.chdir(address)
for item in os.listdir(os.curdir):
if os.path.isdir(item):
find_txt(item)
os.chdir(os.pardir)
if os.path.splitext(item) == '.txt':
list1+=os.getcwd() + os.sep + item
print(list1)
address = 'C:/Users/34292/Desktop/abc'
find_txt(address)
页:
[1]