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+ ...
随机推荐
- linux Table is marked as crashed and should be repaired
linux Table is marked as crashed and should be repaired 2014-07-28 10:34 1282人阅读 评论(0) 收藏 举报 分类: da ...
- 查看局域网内所有IP的方法
1,windows下查看局域网内所有IP的方法: 在MS-DOS命令行输入arp -a 2,Linux下,查看局域网内所有IP的方法: 在命令行输入nmap -sP 172.10.3.0/24
- 微软MSDN原版Windows Server 2008 R2 With SP1下载
Windows Server 2008 R2是windows 服务器版本Windows Server 2008 R2继续提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,其中有不少功 ...
- docker 快速部署ES集群 spark集群
1) 拉下来 ES集群 spark集群 两套快速部署环境, 并只用docker跑起来,并保存到私库. 2)弄清楚怎么样打包 linux镜像(或者说制作). 3)试着改一下,让它们跑在集群里面. 4) ...
- 安卓APP环境搭建
https://www.cocos.com/creator 下载2.0.8 安装的时候选择原生环境 下载SDK:http://tools.android-studio.org/index.php/sd ...
- openFileOutput和openFileInput还有FileOutStream与openFileOutput
FileOutputStream fos = openFileOutput("",MODE_PRIVATE); fos.write("private".getB ...
- MapReduce作业的工作原理
在Hadoop中,我们可以通过Job对象的submit()方法来运行MapReduce作业,也可以调用waitForCompletion()用于提交以前没有提交过的作业,并等待它的完成.其中,subm ...
- [ZZ]MTSC 2017 Mobile Testing@Google 演讲的感受
原文地址: https://testerhome.com/topics/9364 Mobile Testing@Google 其实在开始听谷歌的张南和潘岩开始演讲前,了解下 Google Test C ...
- 未来-IOT-Aliyun:阿里云 IOT - 开发者社区
ylbtech-未来-IOT-Aliyun:阿里云 IOT - 开发者社区 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://dev.iot. ...
- 2018-2019-2 20165312《网络攻防技术》Exp5 MSF基础应用
2018-2019-2 20165312<网络攻防技术>Exp5 MSF基础应用 目录 一.知识点总结 二.攻击实例 主动攻击的实践 ms08_067 payload/generic/sh ...