python中文件类的应用
|
python中对文件、文件夹的操作需要涉及到os模块和shutil模块。 创建文件: 1) os.mknod("test.txt") 创建空文件 2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件 创建目录: os.mkdir("file") 创建目录 os.makedirs('file') 创建多级目录 复制文件: shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件 shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录 复制文件夹: shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在 重命名文件(目录) os.rename("oldname","newname") 文件或目录都是使用这条命令 移动文件(目录) shutil.move("oldpos","newpos") 删除文件 os.remove("file") 删除目录 os.rmdir("dir") 只能删除空目录 shutil.rmtree("dir") 空目录、有内容的目录都可以删 转换目录 os.chdir("path") 换路径 判断目标 os.path.exists("goal") 判断目标是否存在 os.path.isdir("goal") 判断目标是否目录 os.path.isfile("goal") 判断目标是否文件 |
os.walk遍历rootdir的根目录,返回父目录名字,文件夹名字列表,文件名字列表
#for parent,dirnames,filenames in os.walk(rootdir):
# for dirname in dirnames:
# print "parent is :"+parent
# print "dirname is :"+dirname
# if dirname.startswith('Day'+)
#
# for filename in filenames:
# print "parent is:"+parent
# print "filename is :"+filename
# print "the full name of the file is:"+os.path.join(parent,filename)#组合在一起
批量从某个目录下复制东西到新的目录:
#coding=UTF-8
import os
import os.path
import shutil
oldrootdir=r'F:\安卓学习\Android'
newrootdir=r'F:\安卓学习\打包代码-送人'
i=10
for olddirname in os.listdir(oldrootdir):
if olddirname.startswith('Day'+str(i)):
print olddirname
i+=1
for olddestname in os.listdir(oldrootdir+'\\'+olddirname):
baseoldname=oldrootdir+'\\'+olddirname
if olddestname.startswith("PPT"):
shutil.copytree(baseoldname+'\\'+olddestname,newrootdir+'\\'+olddirname+'\\'+olddestname)
if olddestname.startswith("作业"):
shutil.copytree(baseoldname+'\\'+olddestname,newrootdir+'\\'+olddirname+'\\'+olddestname)
批量删除目录:
#coding=UTF-8
import os
name=r'F:\安卓学习\打包代码-送人'
i=1
for dirname in os.listdir(name):
if dirname.__eq__('DAY0'+str(i)):
os.rmdir(name+'\\'+dirname)
i+=1
判断大小筛选:
#coding=UTF-8
import os
name=r'F:\代码Demo习题集\python\python代码\爬虫'
for jpgname in os.listdir(name):
if jpgname.endswith('.jpg'):
size=os.path.getsize(jpgname)
if size<200000:
os.remove(jpgname)
python中文件类的应用的更多相关文章
- Python基础之:Python中的类
目录 简介 作用域和命名空间 class 类对象 类的实例 实例对象的属性 方法对象 类变量和实例变量 继承 私有变量 迭代器 生成器 简介 class是面向对象编程的一个非常重要的概念,python ...
- Python中的类、对象、继承
类 Python中,类的命名使用帕斯卡命名方式,即首字母大写. Python中定义类的方式如下: class 类名([父类名[,父类名[,...]]]): pass 省略父类名表示该类直接继承自obj ...
- python中的类和实例
今天花了两个多小时后搜索相关博客看了看python中有关类和实例的介绍,差不多大概明白了. python中的类和c++中的类是一样的,不同之处就是c++的类,如果含有成员变量,并且成员变量发生变化后, ...
- python中文件的复制
python中文件的复制 python的os模块有很多文件目录相关的函数,但没有提供直接复制文件的函数,当然可以通过边都边写的方式复制文件.想要直接复制文件可以通过shutil模块 shutil模块是 ...
- python 中文件输入输出及os模块对文件系统的操作
整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作. 文件输入输出 1.内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象. 2. ...
- 4、Python中的类详解(0601)
<大话数据结构>的作者程杰在博客园也有博客,网址是:http://cj723.cnblogs.com/ 面向对象编程(OOP) 1.程序 = 指令 + 数据 代码可以选择以指令为核心或以数 ...
- Python中的类(上)
在Python中,可以通过class关键字定义自己的类,然后通过自定义的类对象类创建实例对象. 例如,下面创建了一个Student的类,并且实现了这个类的初始化函数"__init__&quo ...
- python中文件变化监控-watchdog
在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http://pytho ...
- python中新式类和经典类
python中的类分为新式类和经典类,具体有什么区别呢?简单的说, 1.新式类都从object继承,经典类不需要. Python 2.x中默认都是经典类,只有显式继承了object才是新式类 Pyth ...
随机推荐
- 利用if else来运行咱们结婚吧
static void Main(string[] args) { while (true) { string ...
- C# Winform 实现自定义半透明loading加载遮罩层
在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法: 效果图如下,正常时: 显示遮罩层时: 自定义遮罩层控件的源码如下: View Row Code 1 usi ...
- 【转】ios中@class和 #import 的使用时机
代码中会发现有部分#import操作写在m文件中,而h文件仅仅使用@class进行声明,为什么不直接把#import放到h文件中呢? 这是因为h文件在修改后,所有import该h文件的所有文件必须重 ...
- 【转】你应该知道的十个VirtualBox技巧与高级特性
原文网址:http://www.searchvirtual.com.cn/showcontent_76463.htm VirtualBox集成的许多功能你可能从来没有使用过,即使你经常用它来运行虚拟机 ...
- 组合计数(polya计数):SGU 282 Isomorphism
因为论文的题解写得太好了,直接贴. #include <iostream> #include <cstring> #include <cstdio> using n ...
- JSP九大内置对象详解
内置对象特点: 1. 由JSP规范提供,不用编写者实例化. 2. 通过Web容器实现和管理 3. 所有JSP页面均可使用 4. ...
- HTML5 Canvas核心技术—图形、动画与游戏开发.pdf6
操作图像的像素:getImageData() putImageData() ImageData对象 调用getImageData()方法实际是获取了一个指向ImageData对象的引用,返回的对象包含 ...
- HW4.25
public class Solution { public static void main(String[] args) { double sum; for(int i = 10000; i &l ...
- What does the “__block” keyword mean?
It tells the compiler that any variable marked by it must be treated in a special way when it is use ...
- gcc 的visibility 使用
gcc 的visibility 使用(zz) -fvisibility=default|internal|hidden|protectedgcc的visibility是说,如果编译的时候用了这个属性, ...