|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今天做小甲鱼的题 只改变了a = ('.txt')和 a=['.txt'], 输出结果为啥差这么多 第二个是我想要的 不知道什么原因 求大神们解答(只改变了红字部分)
1、
import os
def sosuo(start):
os.chdir(start)
a = ('.txt')
for each_path in os.listdir(os.curdir):
if os.path.splitext(each_path)[1] in a:
b.append(os.getcwd()+os.sep+each_path+'\n')
if os.path.isdir(each_path):
sosuo(each_path)
os.chdir(os.pardir)
return b
def chuanjian(start,b):
a = open(start+'\\a.txt','wt')
a.writelines(b)
a.close
start = input('请输入待查找的初始目录:')
b = []
c = sosuo(start)
chuanjian(start,c)
输出结果为:
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\3
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\3\1
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\3\1\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\4
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\4\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2\5
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2\6
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2\6\7
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2\6\7\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\a.txt
2、
import os
def sosuo(start):
os.chdir(start)
a = ['.txt']
for each_path in os.listdir(os.curdir):
if os.path.splitext(each_path)[1] in a:
b.append(os.getcwd()+os.sep+each_path+'\n')
if os.path.isdir(each_path):
sosuo(each_path)
os.chdir(os.pardir)
return b
def chuanjian(start,b):
a = open(start+'\\a.txt','wt')
a.writelines(b)
a.close
start = input('请输入待查找的初始目录:')
b = []
c = sosuo(start)
chuanjian(start,c)
输出结果为:
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\3\1\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\1\4\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\2\6\7\1.txt
C:\Users\nirui\Desktop\python��ϰ\20200601\2\a.txt
我知道为什么了 ... 我因为你加上括号等于没加
如图:
|
|