python抓取文件夹的所有文件

yipeiwu_com6年前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设计】。

相关文章

python3简单实现微信爬虫

使用ghost.py 通过搜搜 的微信搜索来爬取微信公共账号的信息 # -*- coding: utf-8 -*- import sys reload(sys) import dat...

Python的Scrapy爬虫框架简单学习笔记

 一、简单配置,获取单个网页上的内容。 (1)创建scrapy项目 scrapy startproject getblog (2)编辑 items.py # -*-...

一步步教你用python的scrapy编写一个爬虫

一步步教你用python的scrapy编写一个爬虫

介绍 本文将介绍我是如何在python爬虫里面一步一步踩坑,然后慢慢走出来的,期间碰到的所有问题我都会详细说明,让大家以后碰到这些问题时能够快速确定问题的来源,后面的代码只是贴出了核心...

Python3实现抓取javascript动态生成的html网页功能示例

本文实例讲述了Python3实现抓取javascript动态生成的html网页功能。分享给大家供大家参考,具体如下: 用urllib等抓取网页,只能读取网页的静态源文件,而抓不到由jav...

详解python 爬取12306验证码

详解python 爬取12306验证码

一个简单的验证码爬取程序 本文介绍了在Python2.7环境下爬取网站验证码: 思路就是获取验证码对应的url,然后发起requst请求,读取该URL对应的内容,然后写入到一个本地文件,...