代码:
#!/usr/bin/python
#
#coding:utf-8
#site: www.jb200.com
import urllib2
import urllib
import os
import shutil
homedir = os.getcwd()
import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name) + 'n')
for name in dirs:
print(os.path.join(name))
fileinfo.write(' ' + os.path.join(root,name) + 'n')
fileinfo = open('list.txt','w')
walk_dir(homedir,fileinfo)
说明:
以上基于python27实现。
将此代码放到需要遍历的目录下运行,则会将目录下所有的文件和子文件写到list.txt中。