递归遍历目录和文件

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. C# WinForm中NotifyICon控件的用法

    参考:http://blog.csdn.net/paullink520/article/details/14170021 http://www.cnblogs.com/webman/archive/2 ...

  2. Linux的telent服务

    目前,在Win/Linux下telnet都很少用了,只是因为数据在传输的过程,未加密!不过在Linux下还是多用于端口探测,今天就来回顾曾经的telnet 1.介绍 Linux被广泛运用到各种服务器及 ...

  3. 事务的四个属性ACID

    事务四大特征:原子性,一致性,隔离性和持久性. 1. 原子性(Atomicity) 一个原子事务要么完整执行,要么干脆不执行.这意味着,工作单元中的每项任务都必须正确执行.如果有任一任务执行失败,则整 ...

  4. 新系统基础优化--Centos6.6

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. NET MVC全局异常处理(一) 【转载】网站遭遇DDoS攻击怎么办 使用 HttpRequester 更方便的发起 HTTP 请求 C#文件流。 Url的Base64编码以及解码 C#计算字符串长度,汉字算两个字符 2019周笔记(2.18-2.23) Mysql语句中当前时间不能直接使用C#中的Date.Now传输 Mysql中Count函数的正确使用

    NET MVC全局异常处理(一)   目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关 ...

  6. Mac添加快捷键开启应用程序(转)

    最近使用终端比较多点,打开终端的方法有几种:比较常用有把终端添加到Dock栏上,然后就是利用Spotlight搜索Terminal来打开.但是两种方式还是让我感觉不太满意. 当开启的程序比较多的时候, ...

  7. 一步一步掌握线程机制(三)---synchronized和volatile的使用

    现在开始进入线程编程中最重要的话题---数据同步,它是线程编程的核心,也是难点,就算我们理解了数据同步的基本原理,但是我们也无法保证能够写出正确的同步代码,但基本原理是必须掌握的. 要想理解数据同步的 ...

  8. C# 获取接口数据(xml格式)转为json格式

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  9. PHP中的$_SERVER超全局变量

    详细参数 PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document ...

  10. postman 脚本学习

    pm的脚本断言库默认似乎是集成chaijs的.所以重点也要掌握chaijs的用法,其实和其他断言库类似.玩着玩着就会了.推荐看看 简书 chaijs 中文文档 传送门: # pm 脚本的教程 http ...