forked from Echo9573/DataAnalysisbyPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadtxt.py
More file actions
57 lines (53 loc) · 1.89 KB
/
readtxt.py
File metadata and controls
57 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding:utf-8 -*-
# Python实现读取目录所有文件的文件名并保存到txt文件代码
# 方法一:使用os.listdir
import os
def ListFilesToTxt(dir,file,wildcard,recursion):
exts = wildcard.split(" ")
files = os.listdir(dir)
for name in files:
fullname=os.path.join(dir,name)
if(os.path.isdir(fullname) & recursion):
ListFilesToTxt(fullname,file,wildcard,recursion)
else:
for ext in exts:
if(name.endswith(ext)):
file.write(name + "\n")
break
def Test():
dir="C:/Users/zhuwenjing/Desktop/dairy/2_summerVacation"
outfile="binaries.txt"
wildcard = ".txt .exe .dll .lib"
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 1)
file.close()
Test()
dir = "C:/Users/zhuwenjing/Desktop/dairy/2_summerVacation"
print os.listdir(dir)
# # 方法2:使用os.walk递归地对目录及子目录处理,每次返回的三项分别为:当前递归的目录,当前递归的目录下的所有子目录,
# # 当前递归的目录下的所有文件。
# import os
# def ListFilesToTxt(dir,file,wildcard,recursion): #recursion控制递归深度,只递归当前目录(*****)
# exts = wildcard.split(" ")
# for root, subdirs, files in os.walk(dir):
# for name in files:
# for ext in exts:
# if(name.endswith(ext)): # (*****)
# file.write(name + "\n")
# break
# if(not recursion):
# break
# def Test():
# dir=r"C:/Users/zhuwenjing/Desktop/dairy/2_summerVacation"
# outfile="binaries.txt"
# wildcard = ".txt .exe .dll .lib"
#
# file = open(outfile,"w")
# if not file:
# print ("cannot open the file %s for writing" % outfile)
# ListFilesToTxt(dir,file,wildcard, 0)
#
# file.close()
# Test()