mkdir(path[, mode=0777])

makedirs(name,mode=511)

rmdir(path)

removedirs(path)

listdir(path)

getcwd()

chdir(path)

walk(top, topdown=True, onerror=None)  返回一个元组:(遍历的路径,目录,文件列表)


我们来遍历一下文件:

 >>> for path, dir, filename in os.walk('/home/branches/python/testdir'):
... for filen in filename:
... os.path.join(path, filen)
...
'/home/branches/python/testdir/f1'
'/home/branches/python/testdir/f2'
'/home/branches/python/testdir/f3'
'/home/branches/python/testdir/jpg/0.ipg'
'/home/branches/python/testdir/jpg/3.ipg'
'/home/branches/python/testdir/jpg/2.ipg'
'/home/branches/python/testdir/jpg/1.ipg'
'/home/branches/python/testdir/jpg/get_img.py'
>>>
>>>
>>>
>>> for path, dir, filename in os.walk('testdir'):
... for filen in filename:
... os.path.join(path, filen)
...
'testdir/f1'
'testdir/f2'
'testdir/f3'
'testdir/jpg/0.ipg'
'testdir/jpg/3.ipg'
'testdir/jpg/2.ipg'
'testdir/jpg/1.ipg'
'testdir/jpg/get_img.py'
>>>
 #!/usr/bin/python
#coding:utf8 import os def dirList(path): # 列出当前目录下的完整路径名
filelist = os.listdir(path)
# fpath = os.getcwd() # 它获取的是程序所在路径
allfile = []
for filename in filelist:
# allfile.append(fpath + '/' + filename)
filepath = os.path.join(path, filename) # 用os模块的方法进行路径拼接
if os.path.isdir(filepath):
dirList(filepath)
print filepath allfile = dirList('/home/branches/python/testdir')

Python之OS(系统操作)模块常用函数的更多相关文章

  1. Python文件或目录操作的常用函数

    ◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...

  2. python重要的第三方库pandas模块常用函数解析之DataFrame

    pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...

  3. $python正则表达式系列(2)——re模块常用函数

    本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...

  4. Python中os和shutil模块实用方法集…

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  5. Python中os和shutil模块实用方法集锦

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  6. pandas模块常用函数解析之Series(详解)

    pandas模块常用函数解析之Series 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网 ...

  7. random模块常用函数

    random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...

  8. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  9. Python os模块常用函数详解

    当前使用平台: os.name #返回当前使用平台的代表字符,Windows用'nt'表示,Linux用'posix'表示 当前路径和文件 os.getcwd() #返回当前工作目录 os.listd ...

  10. python ftp操作脚本&常用函数

    需求:快速进行ftp上传 ,下载,查询文件 原来直接在shell下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...

随机推荐

  1. Linux Kernel中所應用的數據結構及演算法

    Linux Kernel中所應用的數據結構及演算法 Basic Data Structures and Algorithms in the Linux kernel Links are to the  ...

  2. java -jar 中文乱码

    java -Dfile.encoding=utf-8 -jar demo.jar 添加编码即可

  3. Shell基本概述

    目录 Shell01--基本概述 1. Shell课程大纲介绍 2. 什么是Shell ? 3. 什么是Shell脚本 ? 4. 为什么要学Shell编程 ? 5. 学习Shell编程需要哪些知识 ? ...

  4. C++中static的作用

    tatic关键字有俩作用:(1).控制存储分配:(2).控制一个名字的可见性和连接.   随着C++名字空间的引入,我们有了更好的,更灵活的方法来控制一个大项目的名字增长.     在类的内部使用st ...

  5. EL属性范围用法sessionScope等(转)

    EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...

  6. firefox下jquery ajax 返回 [object XMLDocument]处理

    在firefox下使用jquery ajax处理 返回json类型的时候,ajax执行成功返回结果为 [object XMLDocument]. 处理办法:在getWriter.write():前面加 ...

  7. 【leetcode】701. Insert into a Binary Search Tree

    题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...

  8. 【leetcode】983. Minimum Cost For Tickets

    题目如下: In a country popular for train travel, you have planned some train travelling one year in adva ...

  9. c# 6.0 语法特性

    namespace _6._0新特性 { using static _6._0新特性.Statics.StaticClass; class Program { static void Main(str ...

  10. 如何更改PHPCMS网站后台标题(title)

    打开PHPCMS安装目录,选择phpcms 然后选择Languages目录,打开. 打开目录后,选择zh-cn目录,选择admin.lang.php用editPlus打开,将第九行后面的引号中的内容换 ...