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

相关文章

Python3网络爬虫之使用User Agent和代理IP隐藏身份

Python3网络爬虫之使用User Agent和代理IP隐藏身份

本文介绍了Python3网络爬虫之使用User Agent和代理IP隐藏身份,分享给大家,具体如下: 运行平台:Windows Python版本:Python3.x IDE...

python解决网站的反爬虫策略总结

本文详细介绍了网站的反爬虫策略,在这里把我写爬虫以来遇到的各种反爬虫策略和应对的方法总结一下。 从功能上来讲,爬虫一般分为数据采集,处理,储存三个部分。这里我们只讨论数据采集部分。 一...

Python实现的爬取百度文库功能示例

本文实例讲述了Python实现的爬取百度文库功能。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- from selenium import webd...

python2.7实现爬虫网页数据

python2.7实现爬虫网页数据

最近刚学习Python,做了个简单的爬虫,作为一个简单的demo希望帮助和我一样的初学者。 代码使用python2.7做的爬虫  抓取51job上面的职位名,公司名,薪资,发布...

讲解Python的Scrapy爬虫框架使用代理进行采集的方法

1.在Scrapy工程下新建“middlewares.py” # Importing base64 library because we'll need it ONLY in cas...