Python之OS(系统操作)模块常用函数
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(系统操作)模块常用函数的更多相关文章
- Python文件或目录操作的常用函数
◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...
- python重要的第三方库pandas模块常用函数解析之DataFrame
pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...
- $python正则表达式系列(2)——re模块常用函数
本文主要介绍正则re模块的常用函数. 1. 编译正则 import re p = re.compile(r'ab*') print '[Output]' print type(p) print p p ...
- Python中os和shutil模块实用方法集…
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- Python中os和shutil模块实用方法集锦
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- pandas模块常用函数解析之Series(详解)
pandas模块常用函数解析之Series 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器输入网 ...
- random模块常用函数
random模块常用函数: from random import * # Random float: 0.0 <= x < 1.0 random() # Random float: 2.5 ...
- AR模块常用函数
--AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...
- Python os模块常用函数详解
当前使用平台: os.name #返回当前使用平台的代表字符,Windows用'nt'表示,Linux用'posix'表示 当前路径和文件 os.getcwd() #返回当前工作目录 os.listd ...
- python ftp操作脚本&常用函数
需求:快速进行ftp上传 ,下载,查询文件 原来直接在shell下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...
随机推荐
- Logstash配置文件详情
logstash 配置文件编写详解 说明 它一个有jruby语言编写的运行在java虚拟机上的具有收集分析转发数据流功能的工具能集中处理各种类型的数据能标准化不通模式和格式的数据能快速的扩展自定义日志 ...
- POJ 3020:Antenna Placement(无向二分图的最小路径覆盖)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6334 Accepted: 3125 ...
- 40.Unique Binary Search Trees(不同的二叉搜索树)
Level: Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...
- js 基础 for in 和 for of的区别详解
for in 和 for of 相对于大家肯定都不陌生,都是用来遍历属性的没错.那么先看下面的一个例子: 例1 const obj = { a: 1, b: 2, c: 3 } for (let i ...
- spring集成rabbitMq(非springboot)
首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...
- 解决 Failed to load class "org.slf4j.impl.StaticLoggerBinder"
我们在使用日志记录网站或者应用时,有时候启动会出现这个告警: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder&q ...
- 一个故事讲懂vue父子组件传值
作者:李佳明同学链接:https://www.jianshu.com/p/2272b6ca0f0c 一个故事讲懂vue父子组件传值 讲故事前先讲代码 父组件向子组件传值 父组件数据传递给子组件可以通过 ...
- Win10桌面图标显示不正常变成了白色
开机不知道什么原因,windows 10 桌面图标全部变成了白色,软件是可以点击正常打开使用,但是看着特别不爽.今天就告诉大家一种办法,解决这种问题. 解决步骤 1.在桌面右键新建 "文本文 ...
- 【leetcode】984. String Without AAA or BBB
题目如下: Given two integers A and B, return any string S such that: S has length A + B and contains exa ...
- 根据一个经纬度坐标获取周边最近经纬。Java实现
1.需求:指定一个经纬度坐标,如:(31.2121751783,121.4411213954).周围有一堆经纬度坐标,找出与它最近的那个. 2.实现思路:将给出经纬度看成原点(0,0).周围经纬度定位 ...