python抓取文件夹的所有文件

yipeiwu_com5年前Python爬虫

本文实例为大家分享了python抓取文件夹的所有文件的具体代码,供大家参考,具体内容如下

#!/user/bin/python 
# -*- coding:utf8 -*- 
 
import Basic 
import os 
 
##################################################### 
########    Input      #### 
folder = "D:\\安装包" 
folder = folder.encode("gbk") 
 
########    Global      #### 
fileList = [] 
allFiles = [] 
tree = [] 
level = 0 
##################################################### 
 
try: 
 if folder is None or len(folder) == 0: 
  folder = os.curdir 
 
 if os.path.isdir(folder): 
  childFile = os.listdir(folder) 
  fileList = ["%s" % (folder+os.path.sep+f) for f in childFile] 
 
  node = {'file':folder, 'level':level} 
  tree.append(node) 
 
  while fileList is not None and len(fileList) > 0: 
   allFiles.append(""+fileList[0]) 
 
   if os.path.isdir(fileList[0]): 
    childFile = os.listdir(fileList[0]) 
 
    if childFile is not None and len(childFile) > 0: 
     fileList = fileList + ["%s" % (fileList[0]+os.path.sep+ft) for ft in childFile] 
    else: 
     pass 
   else: 
    pass 
 
   fileList.pop(0) 
 
  print "\n".join(["%s" % f for f in allFiles]) 
 else: 
  print 'not folder, no child' 
 
except Exception,x: 
 print x 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python抓取网页时字符集转换问题处理方案分享

问题提出:     有时候我们采集网页,处理完毕后将字符串保存到文件或者写入数据库,这时候需要制定字符串的编码,如果采集网页的编码是gb2312,而我们的数...

Python代理IP爬虫的新手使用教程

Python代理IP爬虫的新手使用教程

前言 Python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程。爬虫的初级阶段,添加headers和ip代理可...

python爬取足球直播吧五大联赛积分榜

本文实例为大家分享了python爬取足球联赛积分榜的具体代码,供大家参考,具体内容如下 使用BeautifulSoup4解析爬取足球直播吧五大联赛积分榜信息; #! /usr/bi...

Python爬取三国演义的实现方法

本文的爬虫教程分为四部:      1.从哪爬 where      2.爬什么 what  &...

Python爬虫实现“盗取”微信好友信息的方法分析

本文实例讲述了Python爬虫实现“盗取”微信好友信息的方法。分享给大家供大家参考,具体如下: 刚起床,闲来无聊,找点事做,看了朋友圈一篇爬取微信好友信息的文章,突发奇想,偷偷看看女朋友...