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+ ...
随机推荐
- 环境搭建--使用pytharm远程调试树莓派
对于Linux和文本编辑器不那么熟悉的小伙伴来说,直接在树莓派中写程序可谓是痛苦万分.本文将介绍如何使用PyCharm远程调试树莓派,并同步当前python文件到树莓派中. 配置环境 首先要在个人电脑 ...
- Robot Framework脚本在jenkins执行完之后无法查看日志
首先确保Robot Framework Plugin已经安装好,构建结束后test result和log.html等日志文件已显示,只是无法打开查看: 修改tomcat配置:vi tomcat/con ...
- 时间规划在Optaplanner上的实现
在与诸位交流中,使用较多的生产计划和路线规划场景中,大家最为关注的焦点是关于时间的处理问题.确实,时间这一维度具有一定的特殊性.因为时间是一维的,体现为通过图形表示时,它仅可以通过一条有向直线来表达它 ...
- Some elementary algorithms on discrete differential geometry(DDGSpring2016 Demos)
I studied the on-line course(http://brickisland.net/DDGSpring2016/) by myself, and here are the scre ...
- 神经网络常用的Numpy功能笔记
数组初始化 x=np.array([[1,2]]) x=np.zeros((2,3)) 生成随机数 w=np.random.randn(2,3) PIL image转换成array img = np. ...
- Java笔记Spring(九)
完整调试springmvc源码 WebApplicationContext = new XmlWebApplicationContext();// XmlWebApplicationContext通过 ...
- cucumber soapui test web services
没有UI,所以不需要Selenium. 首先Cucumber: 每个web service形成一个feature文件,描述完成的业务场景. 是否引入参数? 如果引入参数,可能需要根据某种方式保存参数, ...
- 开发环境 pyenv
pyeny githup地址:https://github.com/pyenv/pyenv/ 安装时使用pyeny提供安装工具进行安装 githup 地址:https://github.com/pye ...
- 通过JS 给这个input加一个事件 获得焦点,回车事件绑定
通过JS 给这个input加一个事件 就是获得焦点就行了 window.onload = function(){ var oInput = document.getElementById(" ...
- svg在vue-cli3中的配置
svg矢量图有时候在项目中用起来当然要比图片用起来好.之前没有用过svg,这次花了一点时间在项目中支持了svg的使用,个人感觉用起来还是没有直接用font class的爽,然后又改成font clas ...