递归遍历目录和文件

import os

path = r'F:\PycharmProjects\basic gram\作业和习题\test'

def getAllFileAndDir(path):
# 获取当前目录下所有文件及文件目录
fileList = os.listdir(path)
# print(fileList) # 遍历fileList列表
for fileName in fileList:
# isdir isfile
# print(fileName)
# 拼接绝对路径
absFile = os.path.join(path,fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
getAllFileAndDir(absFile)
else:
print(absFile+'---文件')
getAllFileAndDir(path)

栈 深度遍历

import collections

def getAllFileAndDir(sourcePath):

    stack = collections.deque()
stack.append(sourcePath)
while len(stack) != 0:
path = stack.pop()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
stack.append(absFile)
else:
print(absFile+'---文件') getAllFileAndDir(path)

队列 广度遍历

def getAllFileAndDir(sourcePath):

    queue = collections.deque()
queue.append(sourcePath) while len(queue) !=0:
path = queue.popleft()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
queue.append(absFile)
else:
print(absFile+'---文件') getAllFileAndDir(path)

复制目录和文件

import os
# 复制目录
def copyDir(sourDir,targetDir):
if not os.path.exists(sourDir):
print("如果源目录不存在,直接停止")
return if not os.path.exists(targetDir):
os.makedirs(targetDir) listName = os.listdir(sourDir)
for dirNameAndFileName in listName:
sourAbsPath = os.path.join(sourDir,dirNameAndFileName)
targetAbsPath = os.path.join(targetDir,dirNameAndFileName)
if os.path.isdir(sourAbsPath):
copyDir(sourAbsPath,targetAbsPath)
if os.path.isfile(sourAbsPath):
# 如果目标文件不存在, 或者 如果该文件已经存在但是文件大小不一样
if (not os.path.exists(targetAbsPath)) or (os.path.exists(targetAbsPath) and (os.path.getsize(sourAbsPath) != os.path.getsize(targetAbsPath))):
rf = open(sourAbsPath,"rb")
wf = open(targetAbsPath,"wb")
while True:
content = rf.read(1024*1024)
if len(content) == 0:
break
wf.write(content)
wf.flush()
wf.close()
rf.close() sPath = r'F:\PycharmProjects\basic gram\作业和习题\test'
tPath = r'F:\PycharmProjects\basic gram\作业和习题\testNew'
copyDir(sPath, tPath)

文件复制实例

1.一个函数接受文件夹的名称作为输入参数,请将该文件夹中的所有文件复制到 文件夹名-副本 中去,请补充缺失的代码. (20分)
def copyFile(sPath)
2.题1复制过程中,每隔一秒打印一次复制进度(即当前已复制个数/总文件个数)(15分)

 
import os
import collections
import time
import sys
def getFileNum(sPath):
num = 0
stack = collections.deque()
stack.append(sPath)
while len(stack) != 0:
path = stack.pop()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
stack.append(absFile)
else:
num += 1
return num def copyFile(sPath):
tPath = r'F:\PycharmProjects\basic gram\作业和习题\Anaconda3-副本'
stack1 = collections.deque()
stack1.append(sPath)
stack2 = collections.deque()
stack2.append(tPath)
timepoint = 1
filenum = 0
while len(stack1) != 0:
sPath = stack1.pop()
tPath = stack2.pop()
if not os.path.exists(tPath):
os.makedirs(tPath)
listName = os.listdir(sPath)
for filename in listName:
absfile = os.path.join(sPath, filename)
tabsfile = os.path.join(tPath, filename)
if os.path.isdir(absfile):
stack1.append(absfile)
stack2.append(tabsfile)
else:
rf = open(absfile, 'rb')
wf = open(tabsfile, 'wb')
while True:
content = rf.read(1024*1024)
if len(content) == 0:
break
wf.write(content)
# 刷新缓冲区
wf.flush()
if time.clock()//1 == timepoint:
sys.stdout.write('\r进度:%d/%d'%(filenum,num))
timepoint += 1
wf.close()
rf.close()
filenum += 1
sys.stdout.write('\r进度:%d/%d' % (num, num)) sPath = r'F:\PycharmProjects\basic gram\作业和习题\Anaconda3' num = getFileNum(sPath)
# print(num)
start_time = time.clock()
copyFile(sPath)

阅读原文

python 文件目录遍历的更多相关文章

  1. python文件目录遍历保存成xml文件代码

    Linux服务器有CentOS.Fedora等,都预先安装了Python,版本从2.4到2.5不等,而Windows类型的服务器也多数安装了Python,因此只要在本机写好一个脚本,上传到对应机器,在 ...

  2. Android安全开发之ZIP文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...

  3. python 实时遍历日志文件

    首先尝试使用 python open 遍历一个大日志文件, 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readlin ...

  4. Android 安全开发之 ZIP 文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在"../"的字符串,攻击者可以利用多个"../"在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原 ...

  5. Python文件遍历二种方法

    分享下有关Python文件遍历的两种方法,使用的OS模块的os.walk和os.listdir实现. 关于Python的文件遍历,大概有两种方法,一种是较为便利的os.walk(),还有一种是利用os ...

  6. Python简单遍历字典及删除元素的方法

    Python简单遍历字典及删除元素的方法 这篇文章主要介绍了Python简单遍历字典及删除元素的方法,结合实例形式分析了Python遍历字典删除元素的操作方法与相关注意事项,需要的朋友可以参考下 具体 ...

  7. python+selenium遍历某一个标签中的内容

    一.python+selenium遍历某一个标签中的内容 举个例子:我要获取列表标签<li></li>的内容 根据python+selenium定位到列表整体,使用for循环获 ...

  8. python栈、队列、文件目录遍历

    一. 栈与队列 关注公众号"轻松学编程"了解更多. 1. 栈 stack 特点:先进先出[可以抽象成竹筒中的豆子,先进去的后出来] 后来者居上 mystack = [] #压栈[向 ...

  9. python中遍历文件的3个方法

    转自: http://www.jb51.net/article/54640.htm 用python进行文件遍历有多种方法,这里列举并说明一下. os.path.walk() 这是一个传统的用法. wa ...

随机推荐

  1. 【DB2】通过db2top找到会话对应的ip地址

    1.进入db2top监控界面 2.按l进入到会话界面 根据上图我们可以看到现在有一个应用在连接数据库,我们可以查看引用的具体信息 3.按a,然后输入应用的ID(ID为第一列对应的数字,上图的ID为29 ...

  2. Ubuntu 12.10安装QQ2012

    [日期:2012-11-05] 在最新的Ubuntu 12.10下安装QQ2012,请根据自己的机器类型下载后按照下面的32位或64位安装说明安装. 下载网址:http://www.longene.o ...

  3. Weex开发之路(1):开发环境搭建

    一.Weex介绍 Weex是阿里巴巴在2016年6月份对外开源的一款移动端跨平台的移动开发工具,Weex的出现让我们的应用既有了Native的性能和H5的动态性,只要通过前端JS语法就能写出同时兼容i ...

  4. Mysql DBA 20天速成教程

    Mysql DBA 20天速成教程 基本知识1.mysql的编译安装2.mysql 第3方存储引擎安装配置方法3.mysql 主流存储引擎(MyISAM/innodb/MEMORY)的特点4.字符串编 ...

  5. 树莓派进阶之路 (005) - 树莓派Zsh安装脚本(原创)

    zsh.sh #!/bin/bash cd #安装zsh sudo apt-get install zsh #查看zsh cat /etc/shells #更改zsh chsh -s /bin/zsh ...

  6. 【MyBatis】MyBatis之分页

    关于MyBatis的搭建可以参见“MyBatis的配置”,MyBatis是对JDBC底层代码的封装,关于Oracle.MySQL.SqlServer的分页可以查看Oracle.SqlServer.My ...

  7. SSM框架配置文件

    1.Spring <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns=&quo ...

  8. block(八)作用域

    //闭包 NSString* s =@"123"; void (^block)() = ^() { NSLog(@"%@",s); }; block();// ...

  9. 你应该知道的JAVA面试题

    你应该知道的JAVA面试题 经常面试一些候选人,整理了下我面试使用的题目,陆陆续续整理出来的题目很多,所以每次会抽一部分来问.答案会在后面的文章中逐渐发布出来. 基础题目 Java线程的状态 进程和线 ...

  10. linux 文件系统 xfs、ext4、ext3 的区别

    前言 centos7.0开始默认文件系统是xfs,centos6是ext4,centos5是ext3 ext3介绍 ext3和ext4的最大区别在于,ext3在fsck时需要耗费大量时间(文件越多,时 ...