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下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...
随机推荐
- NOIP2015D1T2 信息传递
题目描述 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 Ti 的同学. 游戏开始时,每人都只 ...
- 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js---转载
jquery.scrollLoading方法 html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...
- 任正非:5G技术只独家卖给美国!不卖给韩国、日本、欧洲
https://v.qq.com/x/page/g3001d0xvxe.html 我只转个标题,细节不管了. 呃,实际上就是说,老任头也决定向美国低头了,对不. 不过,也确实没办法. 该起床吃钙片了.
- 轻松上手nodeJs爬取想要页面的数据
开始之前请先确保自己安装了Node.js环境!!!!!!!! 1.在项目文件夹安装两个必须的依赖包 npm install superagent -S SuperAgent(官网是这样解释的) --- ...
- javascript基础7(正则及表单验证)
1.正则的概念 JS诞生的目的是什么? 就是为了做表单验证. 在JS未出现以前,表单的信息验证需要传输给后台,让后台做数据验证处理之后,再返回给前端页面处理的结果.在带宽有 ...
- windows server 进入组策略管理
方法: win+R 然后输入 gpmc.msc 即可在域服务器上进行组策略管理了.
- 多个ip地址获取
#include "stdafx.h"#include <stdio.h> #include <winsock.h> #include <window ...
- 不修改源代码,动态注入Java代码的方法(转)
转自:https://blog.csdn.net/hiphoon_sun/article/details/38707927 有时,我们需要在不修改源代码的前提下往一个第三方的JAVA程序里注入自己的代 ...
- Oracle修改数据文件路径
更改Oracle数据文件名及数据文件存放路径 SQL> select * from v$dbfile; FILE# NAME---------- ------------------- ...
- atcoder 泛做
https://atcoder.jp/contests/arc060/tasks/arc060_b 先考虑一些特殊情况: $$n>s$$ $$n=s$$ $b$小于$sqrt(N)$可以枚举,如 ...