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使用requests模块爬取页面内容的实战演练

python3使用requests模块爬取页面内容的实战演练

1.安装pip 我的个人桌面系统用的linuxmint,系统默认没有安装pip,考虑到后面安装requests模块使用pip,所以我这里第一步先安装pip。 $ sudo apt i...

python实现爬虫统计学校BBS男女比例(一)

python实现爬虫统计学校BBS男女比例(一)

一、项目需求 前言:BBS上每个id对应一个用户,他们注册时候会填写性别(男、女、保密三选一)。 经过检查,BBS注册用户的id对应1-300000,大概是30万的用户 笔者想用Pyth...

python基于BeautifulSoup实现抓取网页指定内容的方法

本文实例讲述了python基于BeautifulSoup实现抓取网页指定内容的方法。分享给大家供大家参考。具体实现方法如下: # _*_ coding:utf-8 _*_ #xiao...

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

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

关于反爬虫的一些简单总结

关于反爬虫的一些简单总结

爬虫与反爬虫,这相爱相杀的一对,简直可以写出一部壮观的斗争史。而在大数据时代,数据就是金钱,很多企业都为自己的网站运用了反爬虫机制,防止网页上的数据被爬虫爬走。然而,如果反爬机制过于严格...