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+ ...
随机推荐
- 数据库if判断语句
THEN '青年' ELSE '未成年' END) as age_text from user 更多写法参考:http://www.cnblogs.com/martinzhang/p/3220595. ...
- php在laravel中使用自定义的Common类
众所周知,laravel是一款高度集成的开发框架,框架内置非常多的操作方法,从而保证了我们的开发效率.但是在日常中为了满足我们的个性化业务,也需要自己去编写工具类,在laravel中我们完成编写后还需 ...
- 【网络】IP子网划分详解
1.IP地址组成 IP地址组成示意图 IP地址由32位二进制组成,32位二进制分成了4字节,每字节8位,字节之间用符.(点)分隔,为了方便 ...
- webpack打包vue -->简易讲解
### 1. 测试环境: 推荐这篇文章:讲的很细致 https://www.cnblogs.com/lhweb15/p/5660609.html 1. webpack.config.js自行安装 { ...
- WPF 开源项目
Modern UI for WPF :http://mui.codeplex.com/ 利用Wpf实现Win8 Modern样式的开源项目wpf toolkit :http://wpftoolkit. ...
- 如何去maven仓库下载jar包
Maven仓库地址 : http://search.maven.org/ https://mvnrepository.com/ 或者你直接百度搜索 : maven仓库 第一个就是 我现在想下载myba ...
- LeetCode——17. Letter Combinations of a Phone Number
一.题目链接: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
- MySQL完整教程(共8章)
正文 [第一章] 回到顶部 1.1 MySQL学习路线 基础阶段:MySQL数据库的基本操作(增删改查),以及一些高级操作(视图.触发器.函数.存储过程等). 优化阶段:如何提高数据库的效率,如索引, ...
- Pandas中Loc用法总结
摘自:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html 具体用法,假设数据源为: > ...
- Vue 编程式导航(通过js跳转页面)以及路由hash模式和history模式
第一种方法: this.$router.push({path:'shopcontent?aid=3'} 第二种方法 this.$router.push({name:'news'}} 通过在ma ...