python入门(九):目录操作
>>> os.getcwd()
'E:\\'
>>> os.chdir("e:\\pic1")
'e:\\pic1'
>>> os.listdir("e:\\blog")
['1.txt', '2.txt', 'a', 'b']
>>> os.path.isdir("e:\\blog\\config.py")
False
>>> os.path.isdir("e:\\blog\\flask")
True
>>> os.path.isfile("e:\\blog\\config.py")
True
>>> os.path.isfile("e:\\blog\\flask")
False
统计一下有多少个目录
提示:用os.listdir 和判断文件和路径的api来进行计数
1 定义文件计数的变量
2 定义文件夹计数的变量
并赋值0
3 使用os.listdir()获取所有的文件和目录名称,这个返回值是列表
如果是文件,则在文件计数的变量上+1,否则在目录计数的变量上加一
5 输出统计结果:文件计数和目录计数的变量值
import os.path
file_count = 0
dir_count =0
for i in os.listdir("e:\\pic1"):
print("取出的内容:",i)
if os.path.isfile("e:\\pic1\\"+i):
file_count+=1
else:
dir_count+=1
print("文件个数:%s" %file_count)
print("目录个数:%s" %dir_count)
>>> os.getcwd()
'E:\\'
>>> os.chdir("e:\\pic1")
>>> os.getcwd()
'e:\\pic1'
>>> os.remove("e:\\pic1\\1.txt")
>>> os.remove("2.txt")
>>> os.remove("2.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: '2.txt'
>>>
>>> os.mkdir("e:\\pic1\\testman1")
>>> os.mkdir("e:\\pic1\\testman")
>>> os.mkdir("e:\\pic1\\testman1")
>>> os.mkdir("e:\\pic1\\testman1\\testman3")
>>> os.mkdir("e:\\pic1\\testman1\\testman4")
目录非空(包含子目录或者文件)就停止删除目录了
'e:\\pic1'
>>> os.path.isabs("1.txt")
False
>>> os.path.isabs("e:\\pic1\\1.txt")
True
... with open("e:\\pic1\\2.txt","w") as fp:
... fp.write("")
os.mkdir("e:\\pic1\\testman11")
算法:
import os
os.chdir("D:\\gloaryroad")
first_dir=os.getcwd()
for i in range(1,11):
last_dir="good"+str(i)
os.mkdir(first_dir+"\\"+last_dir)
first_dir+="\\"+last_dir
os.chdir(first_dir)
with open(str(i)+".txt","w") as fp:
fp.write(str(i)+".txt")
--------------------------------------------------
dir_name = "glroyroad"
os.chdir("e:\\pic1")
for i in range(1,11):
os.mkdir(dir_name+str(i))
os.chdir(dir_name+str(i))
with open(str(i)+".txt","w") as fp:
fp.write(str(i)+".txt")
'e:\\pic1'
>>> os.path.basename(r"e:\pic1\1.txt")
'1.txt'
('e:\\pic1\\1', '.txt')
os.name'nt'
>>> os.listdir()
['1.txt', '200.txt', 'glroyroad1', 'pic2', 'test', 'testman11', 'testmanxxxx', '
新建文本文档.txt']
>>> os.path.getsize("e:\\b.py")
223
python入门(九):目录操作的更多相关文章
- Python入门篇-文件操作
Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...
- python文件及目录操作
python文件及目录操作 读/写文件 新建/打开文件 写入 #举个例子,打开D:\test\data.txt #以写入模式打开文件 #如果test(上级目录)不存在则报错 #如果data.txt(文 ...
- Python入门之 字符串操作,占位符,比较大小 等
Python 字符串 常用的操作 切片 左包括右不包括的原则 ________________ 比较字符串大小 eg: cmp("a",'b') -1第一个比第二个小 0 ...
- python入门7 字符串操作
字符串操作 #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 字符串操作 ""&quo ...
- python 文件和目录操作题库
1. 把一个目录下所有的文件删除,在所有的目录下新建一个a.txt的文件,并在文件下写入"python"关键字. 解题思路: 1.如果目录存在则切换进入目录 ...
- Python - os 文件/目录操作
最近经常用到 os 操作文件/目录,感觉挺好使的,但是一直没有系统的梳理学习一下, 今天想借此机会整理一下工作中常用的方法,也算自己总结学习的一个积累吧. 直接上代码,注释明了 #-*-coding: ...
- python 文件及目录操作
文件的基本操作 >>> import os #文件操作导入的模块 >>> import os.path >>> os.name #操作系统的类型, ...
- Python 文件和目录操作学习
文件与文件路径 文件有两个关键属性:文件名和路径. 路径指明了文件在计算机上的位置. 文件名中,最后一个句点之后的部分称为文件的"扩展名",它指出了文件的类型 目录也叫文件夹,文件 ...
- Python文件或目录操作的常用函数
◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...
- python 基础(九) 文件操作
文件操作 一.函数: f = open(’文件名','打开方式'[,encoding='字符编码']) open 打开的方式 字符 说明 r 只读的方式打开 rb 以二进制的形式打开文件 只读 r+ ...
随机推荐
- 【rabbitmq】rabbitmq集群环境搭建
安装rabbitmq-server 总共有3台虚拟机,都安装有rabbitmq服务,安装过程可参考: [rabbitmq]Centos7 下安装rabbitmq 创建用户和vhost 说明: 此步骤不 ...
- [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受
原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...
- SSIS 中将csv 文件批量导出到excel 文件,并设置excel 文件中某些列的data column format 为Text
csv 文件是文本文件类型,但是打开csv 文件后(默认使用本地已经安装的excel 来打开excel 文件),默认显示出来的是general 类型(column data format)的数据, 这 ...
- java.util.concurrent包下并发锁的特点与适用场景
序号 类 备注 核心代码 适用场景 1 synchronized 同步锁 并发锁加在方法级别上,如果是单例class对象,则只能允许一个线程进入public synchronized void doX ...
- (最详细)小米Note 2的usb调试模式在哪里开启的教程
当我们使用安卓手机链接Pc的时候,或者使用的有些工具比如我们学院营销团队当使用的工具引号精灵,之前使用的老版本就需要开启USB开发者调试模式下使用,现当新版本不需要了,如果手机没有开启USB开发者调试 ...
- SerialPort项目配置
app的build.gradle下: apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultCon ...
- 使用Oracle BBED修改Oracle11g数据库实例名称
by 蔡建良 2019-2-19 数据库名称存在SYSTEM01.DBF表空间,所以先查出你要修改的数据库的DBID和DBNAME. 一. 查询数据库实例名称 加载ORCL实例数据库的SYSTEM01 ...
- SAS-决策树模型
决策树是日常建模中使用最普遍的模型之一,在SAS中,除了可以通过EM模块建立决策树模型外,还可以通过SAS代码实现.决策树模型在SAS系统中对应的过程为Proc split或Proc hpsplit, ...
- EXT.net 图标靠右排列
toolbar1.Items.Add(Button_1); toolbar1.Items.Add(Button_2); toolbar1.Items.Add(Button_3); toolbar1.I ...
- [URLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; _sessio ...