os模块主要处理文件目录(文件夹)的创建、删除、检查判定、属性值修改、路径修改。

(1)获取当前目录的两种方法

1 os.getcwd()
 os.path.abspath('.')

(2)创建文件夹

os.makedirs(r'd:\test')

若该文件夹已存在,会报错。

(3)列举指定目录下的全面文件及文件夹

os.listdir(r'd:\test')

返回值为列表形式

(4)删除空文件夹的两种方法

1 os.removedirs(r'd:\test')
2 os.rmdir('d:\\test')

若制定路径下文件夹不存在和非空文件夹,都会报错。

若程序当前目录在该删除路径内,也会报错。

练习1:在指定目录,批量生成N个文件夹

 import os

 sPath = 'd:\\test'  #指定目录
dirs_name = ['one', 'two', 'three'] #指定文件夹名称 if not os.path.exists(sPath):
os.mkdir(sPath) os.chdir(sPath) for i in range(len(dirs_name)):
if not os.path.exists(str(i+1) + ' ' + dirs_name[i]):
os.mkdir(str(i+1) + ' ' + dirs_name[i]) print('Success')

练习2:打印指定目录下,全部文件的绝对路径(两种方法)

 #!/urs/bin/env python
# -*- coding:utf-8 -*- def print_directory_contents(sPath):
import os
for sChild in os.listdir(sPath):
sChildPath = os.path.join(sPath, sChild)
if os.path.isdir(sChildPath):
print_directory_contents(sChildPath)
else:
print(sChildPath) print_directory_contents(r'D:\test')

实现方式2:

 def print_files_abspath(sPath):
for root, dirs, files in os.walk(sPath):
for name in files:
print(os.path.join(root, name)) print_files_abspath('D:\StormMedia')

练习3:查找指定目录下,全部满足条件的文件,以列表方式呈现文件的绝对路径

 #!/urs/bin/env python
# -*- coding:utf-8 -*- import os result = [] def find_all_files(sPath, fileName):
for root, dirs, files in os.walk(sPath):
for name in files:
if name == fileName:
result.append(os.path.join(root, name)) if __name__ == '__main__':
find_all_files(r'D:\\Linux', 'Makefile')
for i in range(len(result)):
print(result[i]) print("Totals: %s" % len(result))

练习4:打印指定目录下,全部目录的绝对路径

 #!/urs/bin/env python
# -*- coding:utf-8 -*- import os def print_all_directories(sPath):
for root, dirs, files in os.walk(sPath):
for name in dirs:
print(os.path.join(root, name)) print_all_directories(r'D:\test')

python os模块(1)的更多相关文章

  1. Python::OS 模块 -- 进程参数

    os模块的简介请参看 Python::OS 模块 -- 简介 os模块的文件和目录操作 Python::OS 模块 -- 文件和目录操作 os模块的进程管理 Python::OS 模块 -- 进程管理 ...

  2. Python::OS 模块 -- 进程管理

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的文件相关操作参看 Python::OS 模块 -- 文件和目录操作 os模块的进程参数 Python::OS 模块 -- 进程参数 ...

  3. Python::OS 模块 -- 文件和目录操作

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...

  4. Python::OS 模块 -- 简介

    OS 模块简介 OS模块是Python标准库中的一个用于访问操作系统功能的模块,OS模块提供了一种可移植的方法使用操作系统的功能.使用OS模块中提供的接口,可以实现跨平台访问.但是在OS模块中的接口并 ...

  5. Python OS模块标准库的系统接口及操作方法

    Python OS模块标准库的系统接口及操作方法 os.name 返回当前操作系统名,定义了'posix','nt','mac','os2','ce','java'(我使用win7/python3.1 ...

  6. python os模块学习

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是wi ...

  7. python os模块详解

    一.Python os模块(Linux环境) 1.1 执行shell命令 os.system('cmd') 执行命令不保存结果 os.popen('command') 执行后返回结果,使用.read( ...

  8. Python OS模块常用功能 中文图文详解

    一.Python OS模块介绍 OS模块简单的来说它是一个Python的系统编程的操作模块,可以处理文件和目录这些我们日常手动需要做的操作. 可以查看OS模块的帮助文档: >>> i ...

  9. Python OS模块重要知识点

    Python OS模块重要知识点 这几点很重要,主要是关于文件路径,我之前踩了很多坑,今天总结一下,方便以后能够避免与path相关的各种坑! 1,首先我们想获取某个文件夹下面的所有文件夹以及文件(不包 ...

  10. python OS 模块 文件目录操作

    Python OS 模块 文件目录操作 os模块中包含了一系列文件操作的函数,这里介绍的是一些在Linux平台上应用的文件操作函数.由于Linux是C写的,低层的libc库和系统调用的接口都是C AP ...

随机推荐

  1. BizTalk开发系列(二十一) Mapping 扩展开发

    BizTalk Map编辑器提供了常用的功能块,比如数据库,字符串,数字计算等功能.可在设计Map时直接使用这些功能块进行扩展.除此之外对于进行复杂的Map处 理,Map 编辑器提供了扩展XSLT,扩 ...

  2. java之main函数(笔记)

    1.标准的main函数形式 对于main函数,只要是 public static void main(String[] args) public static void main(String... ...

  3. 浅谈Service

    一.生命周期: startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy():( ...

  4. 将公网IP自动发到Twitter上

    [Twitter] 1. 在https://apps.twitter.com/创建新的应用 2. 在https://dev.twitter.com/rest/reference/post/status ...

  5. request

    social.Favorites.AddFavorite=function(angel) { a = $.extend(true, { type: "POST", url: &qu ...

  6. java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  7. 解决git .ignore文件无效

    在用 Git 进行代码管理的时候,我们会用 .gitignore 文件来描述哪些文件是不需要进行版本管理的,也就是被忽略掉. 如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次 ...

  8. SQL的inner join、left join、right join、full outer join、union、union all

    主题: SQL的inner join.left join.right join.full outer join.union.union all的学习. Table A和Table B表如下所示: 表A ...

  9. What is the difference between parameterized queries and prepared statements?

    Both parameterized queries and prepared statements are exactly the same thing. Prepared statement se ...

  10. jQuery的$.get和$.ajax函数对比

    $.get较为简便,但在精细控制上乏力$.get(    url, // 请求的地址    {url:url,remark:remark},// 请求参数    function(data,textS ...