Python 文件夹及文件操作
import os
import os.path
from shutil import copy def copyfile(src, dst):
count = 1
for filename in os.listdir(src):
file = os.path.join(src,filename)
copy(file, dst)
count += 1
#if count == 40000:
# break
return src = "/home/data/fac"
dst = "/home/data/ob/"
copyfile(src=src, dst=dst)
我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数。
os 可以执行简单的文件夹及文件操作,引入用 import os,可用 help(os) 或是 dir(os) 查看其用法。注意有些函数在os模块中,有的是在os.path模块中。
shutil 模块提供了大量的文件的高级操作,特别针对文件拷贝和删除。主要功能为目录和文件操作以及压缩操作。须引入 import shutil ,具体 help。本文仅介绍移动、复制及删除。
可先在 D:\ 下创建文件夹 Python_os , 再在其下创建文件夹 os, 再在其下创建 test.txt;之后的示例会在该文件夹下操作
判断路径或文件
os.path.isabs(...) # 判断是否绝对路径
os.path.exists(...) # 判断是否真实存在
os.path.isdir(...) # 判断是否是个目录
os.path.isfile(...) # 判断是否是个文件
注意: 把两个路径合成一个时,不要直接拼字符串,而要通过 os.path.join(part1,part2)
函数,这样可以正确处理不同操作系统的路径分隔符。在Linux/Unix/Mac下,os.path.join()
返回这样的字符串: part1/part2
而Windows下会返回这样的字符串: part1\part2
1 import os
2 import shutil
3
4 file_dir = "D:\\Python_os\\os" # 注意 \\ ;windows 下是这么表示的;Linux 和 Mac 是 /
5 file_name = "test.txt"
6 file_abs = os.path.join(file_dir, file_name) # os.path.join(...) 表示路径链接
7
8
9 '''判断路径或文件'''
10 print (1,os.path.isabs(file_dir)) # 判断是否绝对路径
11 print (2,os.path.isabs(file_name))
12 print (3,os.path.isabs(file_abs))
13 print (4,os.path.exists(file_abs)) # 判断是否真实存在
14 print (5,os.path.exists(os.path.join(file_dir,"xxx")))
15 print (6,os.path.isdir(file_dir)) # 判断是否是个目录
16 print (7,os.path.isdir(file_abs))
17 print (8,os.path.isfile(file_dir)) # 判断是否是个文件
18 print (9,os.path.isfile(file_abs))
运行结果: aaarticlea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rK7iVM3YrKQEuwwAQ6aXBdwTi3U2sf1k91hePUE4BrQ8dJQiIPusLJDNfhVHio/oyAk2CBCexPuszaScVBndyYbD0BuDYCx10VBw1/itMhWSf5CyEkWHhCzU/yl4dyMkOhPsgXzqsIIcHHCfZPApAuHq43sLYOgBQBJwFIFzpOqhuown6g+qayrqn6qsglq98KW5nIlQdAC9rJUE+t+t1Ql+gM88jQv2/gX5fxjzyFKgdOgsR4uN44ufDNT7ov4mig46SizMDSFIK5BTIvhPeSeRgoMHM7/b8FgABkTtLoCGZoa6wvmOwcxfmByqlPoP8WdYGBfxQAEWGcpFHbxR/Xzy3qExR3VzsvLFnnfLr+/E9hPQP/KAAionDSRf3A8Y+v+tnVdDIC6s8I5qBa6UC9+SN8gQBEwXWSf0A1n7lQjkVwMlQ1hF6phQn8k0PVVucEAFTo9CfVuA9uoDyBpylkE95XcZAvSn2+4rX6pvxfJ7wQAF0UTmomDYVCwteKx1qIvrr8WxnRp4Dsvgqf1fWEjSBJ1E4yJ/PG8qepSwgsVud8ncL5ugU6LPspPD+wfAAiEjNP8pfI3lIcDHRShqJw1xnFhXxt+bcCrZOVBkBEIudJ2UFhDlEXovk065wW9gNC81rZ+YEfFoqPD3WZMzofLABCJxVJhi9B/6DsLbW9oT4mmPpr3kXnRsz5ivsGnqxgducL/zUhcxrhnXT/nTTlkZ2mNi2wWGE5wtfMQYWWTOHMQxkov+wh1pT5eoGTC4Nm21X4iCsO8q/5q9RPieKjQXGCUOPAWyjOVJwvO5jOB139p4EUoV5bp/iVPyj8N07kkU3n8ySrw7VXTEg6/xsCATpr6wAA8wNOApAuvluDkwCkCTgJQLqAkwCkCzgJQLqAkwCkCzgJQLqAkwCkCzgJQLqAkwCkCzgJQLqAkwCkCzgJQLqAkwCkCzgJQKr4P+iAGjbNCXNKAAAAAElFTkSuQmCC" alt="" />
路径名、文件名分隔
os.path.split(...) # 分隔目录和文件名/文件夹名
os.path.splitdrive(...) # 分隔盘符(windows系统)
os.path.splitext(...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O4BgKBQGxhqKlwTZwBMS82iWtnFlDJs8w8i7/SDcY4eBAIlK0w1EiuafV88bdam6hcj+dJvV7p3uUWr/pr+QGugUCrF4ZaLF8jO3kSlKhwTeBT6k0AKewwtsCsi8cyKQRj1anvCwgEykpSrpFSgZSljEJ1SPHKCMpLsSUuQO6L2KF0p0Ag0Dc7ASIAAAIqSURBVLKFofbpXoN3fU1MKHq9eo4jLiCoXcxNpmeV8mT89CczTulOgUCgZQtDTcA1LHGnpREg7v+KXDOQmLOxlWIsShFJr6EdgkCgpQpDDXON7uSK/VaLUwZc0wqDySYxdKS7rBWtSgEQCJSJaK6h9QZ9End+KYCkxQTAYtYrWEm7EpcXLIsrpfeOuSEIBMpcGGoCrikmLwIMMZcFaGBKHX/0VzkWSXn1CpgojhOIBgKthRS5FtuKph5dTOxB6lalvIpzOjYpB3mfzPJS/yAQaNmac203Ub5Gb8L7SrBSyjWeBM4xdwQb0tHSX0nJxfMGAoGWrYd79Yd79U93JVyLbaXCKWYuI3aiSASVYrqQVdyWV14KXAGCxT4zKg8CbbPEXBMkO7Qr9ZW8r8QE1EJtLH7FWlQqipUX1CstLFB25ZlHE4AI2io93Ks/3K1/utt4SHENn+uKAOIVE9NK6pbph7kcWylAW8x5rGNLAcoDgSIQVyvgGmj79XC3/nC3/sluneYaLkMvx7qBYCW9TG8l7mkCvAoKMFEorUJQUlCet3I9YSHeNRBoG4SghuymNeE9RyX4k17J7CepdPv17JO8GFYeGFPr2YYgUJpS4RoIBAJtkj4BroFAoC0TcA0EAm2bgGsgEGjbBFwDgUDbJuAaCATaNgHXQCDQtgm4BgKBtk3ANRAItG365EUNuAYCgbZKn7yoYbQB10Ag0DYIuAYCgbZNwDUQCLRl+n9IGxj1ieByqQAAAABJRU5ErkJggg==" alt="" />
这些合并、拆分路径的函数并不要求目录和文件要真实存在,它们只对字符串进行操作。
工作目录及创建文件夹操作
os.getcwd() # 获取当前工作目录
os.chdir(...) # 改变工作目录
os.listdir(...) # 列出目录下的文件
os.mkdir(...) # 创建单个目录 注意:创建多级用 os.makedirs()
os.makedirs(...) # 创建多级目录
1 import os
2
3 file_dir = "D:\\Python_os\\os"
4
5 print (os.getcwd()) # 获取当前工作目录
6 os.chdir(file_dir) # 改变工作目录
7 print (os.getcwd())
8 print (os.listdir(file_dir)) # 列出当前工作目录的所有文件 Python2 不支持 os.listdir() Python3 会列出当前工作目录下的所有文件
9 os.mkdir("test_mkdir") # 在当前工作目录下创建文件夹 test_mkdir;注意不可存在相同文件夹,不然会报错
10 os.makedirs("test_mkdir\\test1")
11 os.chdir(".\\test_mkdir") # . 表示本级目录; .. 表示上级目录
12 print (os.getcwd())
13 for i in range(2,6): # 使用for循环等,可方便的创建多个文件夹
14 dir_name = "test" + str(i)
15 os.mkdir(dir_name)
在执行了上述实例代码后,os 文件夹中新建了空的 test_mkdir 文件夹,而 test_dir 文件夹下也新建出了 test1 至 test5 的空文件夹
创建文件夹可能会出错,原因具体有:(1) path 已存在时(不管是文件还是文件夹) (2) 驱动器不存在 (3) 磁盘已满 (4) 磁盘是只读的或没有写权限
文件的创建及操作参见下一章节
删除文件夹/文件
os.rmdir(...) # 删除空文件夹 注意:必须为空文件夹 如需删除文件夹及其下所有文件,需用 shutil
os.remove(...) # 删除单一文件
shutil.rmtree(...) # 删除文件夹及其下所有文件
在上方示例的文件夹基础上,操作删除 test1 文件夹 (空文件夹可用 os.rmdir() ),删除 test_mkdir 及其下所有文件();示例代码如下
1 import os
2 import shutil
3
4 file_dir = "D:\\Python_os\\os"
5
6 ''' 删除文件/文件夹 '''
7 os.chdir(file_dir+"\\test_mkdir")
8 print(os.getcwd()) # 确保当前工作目录
9 print(os.listdir(os.getcwd())) # 查看当前文件夹下所有文件
10 os.rmdir("test1") # 删除 test1 文件夹(空文件夹)
11 print(os.listdir(os.getcwd()))
12 os.chdir("..\\")
13 print(os.getcwd()) # 切换到上级目录
14 print(os.listdir(os.getcwd()))
15 shutil.rmtree("test_mkdir") # 删除 test_mkdir 及其下所有文件
可见运行结果如下;产生异常的可能原因: (1) 路径不存在 (2) 路径子目录中有文件或下级子目录(os.rmdir) (3) 没有操作权限或只读
只是删除单一文件,则用 os.remove("test.txt") 即可;产生异常的可能原因: (1) 文件不存在 (2) 对该文件没有操作权限或只读。
重命名文件夹/文件
可对某一文件或文件夹重命名 os.rename(oldfileName, newFilename)
在os文件夹中新建文件夹 test,文件 test.txt
1 ''' 重命名文件夹/文件 '''
2 os.chdir(file_dir)
3 print(os.listdir(os.getcwd()))
4 os.rename("test","test1")
5 os.rename("test.txt","test1.txt") # 重命名,注意需要带扩展名
6 print(os.listdir(os.getcwd()))
可见运行结果如下;产生异常的可能原因: (1) oldfilename 旧文件名不存在(文件须带扩展名) (2)newFilename 新文件已经存在
注意:新文件的扩展名不能遗漏,理论上需要保持类型一致;但这也不失为改文件类型的一种方式(相当于直接改文件的扩展名)
复制、移动文件夹/文件
须使用 shutil 模块,引入 import shutil
shutil.copyfile("old","new") # 复制文件,都只能是文件
shutil.copytree("old","new") # 复制文件夹,都只能是目录,且new必须不存在
shutil.copy("old","new") # 复制文件/文件夹,复制 old 为 new(new是文件,若不存在,即新建),复制 old 为至 new 文件夹(文件夹已存在)
shutil.move("old","new") # 移动文件/文件夹至 new 文件夹中
我们现在 D:\ 下创建新文件夹 Python_shutil ,然后工作目录切到该目录,直接使用 Python 来操作吧,参考代码如下:
1 import os
2 import shutil
3
4 os.chdir("D:\\")
5 os.mkdir("Python_shutil")
6 file_dir = "D:\\Python_shutil"
7 os.chdir(file_dir)
为演示复制、移动操作,我们在该文件夹下手动操作 新建文件夹 test_org、 test_copy 及 test_move; 新建文件 test.txt test1.txt , 两者内容不同
Python 复制、移动的操作示例代码如下:
1 import os
2 import shutil
3
4 file_dir = "D:\\Python_shutil"
5 os.chdir(file_dir)
6 shutil.copyfile("test_org.txt","test_copy.txt") # copy test_org.txt 为 test_copy.txt 若存在,则覆盖
7 shutil.copyfile("test_org.txt","test1.txt") # 存在,覆盖
8 shutil.copytree("test_org","test_copytree") # copy test_org 为 test_copytree(不存在的新目录)
9 shutil.copy("test_org.txt","test_copy1.txt") # 同 copyfile
10 shutil.copy("test_org.txt","test_copy") # 将文件 copy 至 目标文件夹中(须存在)
11 shutil.copy("test_org.txt","test_xxx") # 将文件 copy 至 目标文件(该文件可不存在,注意类型!)
12 print os.listdir(os.getcwd())
13 shutil.move("test_org.txt","test_move") # 将文件 move 至 目标文件夹中
14 shutil.move("test_org","test_move") # 将文件夹 move 至 目标文件夹中
15 print os.listdir(os.getcwd())
出处:http://www.cnblogs.com/feeland/
本博客内容大多为作者原创 如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客
如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]
Python 文件夹及文件操作的更多相关文章
- Python学习(九)IO 编程 —— 文件夹及文件操作
Python 文件夹及文件操作 我们经常会与文件和目录打交道,对于这些操作,python可以使用 os 及 shutill 模块,其中包含了很多操作文件和目录的函数. os 可以执行简单的文件夹及文件 ...
- python文件夹遍历,文件操作,获取文件修改创建时间
在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前 ...
- python文件、文件夹的相关操作
python文件.文件夹的相关操作 #1.rename()可以完成对文件的重命名 #rename(需要修改的文件名,新的文件名) import os os.rename("readme.tx ...
- Python文件夹与文件的操作(转)
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- Python文件夹与文件的操作 ZZ
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- Python对文件和文件夹的高级操作模块shutil
shutil模块提供了许多关于文件和文件夹的高级操作. 特别提供了支持文件复制和删除的功能. # 将文件对象fsrc的内容复制到文件类对象fdst.length(可选参数)是缓冲区大小 shutil. ...
- python 文件,文件夹,路径操作
判断路径或文件os.path.isabs(...) # 判断是否绝对路径os.path.exists(...) # 判断是否真实存在os.path.isdir(...) # 判断是否是个目录os.pa ...
- day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...
- python调用另一个文件中的代码,pycharm环境下:同文件夹下文件(.py)之间的调用,出现红线问题
如何调用另一个python文件中的代码无论我们选择用何种语言进行程序设计时,都不可能只有一个文件(除了“hello world”),通常情况下,我们都需要在一个文件中调用另外一个文件的函数呀数据等等, ...
随机推荐
- Spring IOC 低级容器解析
1.IOC是什么 IOC-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不 ...
- Unicode转义序列
声明: web前端学习笔记,欢迎大神指点.联系QQ:1522025433. Javascipt 定义了一种特殊序列,使用6位ASCII字符代表任意16Unicode内码.这些Unicode转义序列均以 ...
- 《转》Pragma: no-cache 对性能的影响
做了下go和java的http性能的简单比较服务端直接输出字符串使用JMeterwindows下 2000的并发,测试结果很出乎意料,go不会这么差吧 研究了半小时,原因如下tomcat的servl ...
- JavaScript实现抽象类与虚方法(六)
一:什么是js抽象类与虚方法 虚函数是类成员中的概念,是只做了一个声明而未实现的方法,具有虚函数的类就称之为抽象类,这些虚函数在派生类中才被实现.抽象类是不能实例化的,因为其中的虚函数并不是一个完整的 ...
- vijos 1128 N个数选K个数 (DFS )
从 n 个整数中任选 k 个整数相加,可分别得到一系列的和 要求你计算出和为素数共有多少种 IN4 33 7 12 19 OUT1 # include <iostream> # inclu ...
- mongodb 备份
本篇文章来自于阿里云技术专家郑涔(明俨)在2018年<Redis.MongoDB.HBase大咖直播大讲堂>技术直播峰会中的分享,该分享整体由四个部分构成: 1.MongoDB备份恢复 2 ...
- 朴素贝叶斯分类算法介绍及python代码实现案例
朴素贝叶斯分类算法 1.朴素贝叶斯分类算法原理 1.1.概述 贝叶斯分类算法是一大类分类算法的总称 贝叶斯分类算法以样本可能属于某类的概率来作为分类依据 朴素贝叶斯分类算法是贝叶斯分类算法中最简单的一 ...
- BZOJ3064 Tyvj 1518 CPU监控 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3064 题意概括 一个序列,要你支持以下操作: 1. 区间询问最大值 2. 区间询问历史最大值 3. ...
- 073 HBASE的读写以及client API
一:读写思想 1.系统表 hbase:namespace 存储hbase中所有的namespace的信息 hbase:meta rowkey:hbase中所有表的region的名称 column:re ...
- 记录一个mysql的case when用法
SELECT wle.*, CASE WHEN '2017-08-10 14:00:00' > wle.et THEN '回看' WHEN wle.st >= '2017-08-10 14 ...