python_os
1. 基本功能的介绍
os模块包含普通的操作系统的功能
2. 常用的变量
(1)os.name
获取正在使用的平台, Windows 返回 nt, Linux或者Unix 返回 posix
3. 常用的方法
(1)getcwd
string = os.getcwd()
获取当前工作目录
(2)getenv
string = os.getenv(varname[, value])
获取环境变量的值,如果环境变量的值不存在,则返回None
(3)listdir
list = os.istdir(path)
获取指定路径下的所有目录和文件
(4)remove
os.remove(path)
删除指定的文件,如何文件不存在,系统报OSError
(5)split
元组 = os.path.split(path)
返回一个路径的目录名和文件名
(6)join
string = os.path.join(path1[, path2[, ...]])
将目录和文件组合成路径
(7)exists
string = os.path.exists(path)\
判断路径是否存在,存在则返回True,不存在,则返回False
(8)isdir
string = os.path.isdir(path)
判断路径是否为目录,如果是,则返回True;否则,则返回False
(9)isfile
string = os.path.isfile(path)
判断路径是否为文件,如果是,则返回True;否则,则返回False
(4)walk(暂无示例)
root, dirs, files = os.walk(top, topdown=True, onerror=None, followlinks=False)
获取一个目录下面的所有路径、目录名、文件名
topdown= True: 代表从上到下遍历,或者说从根遍历到叶子
topdown= False: 代表从下到上遍历,或者说从叶子遍历到根
4. 示例
#-*- coding:utf-8 -*- import os #获取正在使用的平台, Windows 返回 nt, Linux或者Unix 返回 posix
print os.name #获取当前工作目录
print os.getcwd() #获取环境变量
print os.getenv("JAVA_HOME") #获取指定目录下,所有文件和文件名
print os.listdir("E:\python_script") #删除指定的文件
try:
os.remove("E:/python_script/test.txt")
except:
print "文件已经被删除"
else:
print "删除文件成功" print os.listdir("E:\python_script") #返回一个路径的目录名和文件名
print os.path.split("E:/python_script/hello.py") #将目录和文件名组合成路径
print os.path.join("E:\python_script", "test.txt") #判断一个路径是否真的存在
print os.path.exists("E:/python_script/hello.py") #判断一个路径是否为目录
print os.path.isdir("E:/python_script/hello.py") #判断一个路径是否为文件
print os.path.isfile("E:/python_script/hello.py")
5. 运行结果
python_os的更多相关文章
- Python_os、os.path、os.shutil使用案例
import os import os.path print(os.path.basename('/Users/c2apple/Desktop/彩屏')) #获取路径的最后一个组成部分 os.path ...
- Python_os模块
os模块:可以处理文件和目录,是Python系统和操作系统进行交互的一个接口 os模块常用方法: os.getcwd(): 获取当前工作目录,(即当前Python脚本工作的目录路径) os.chdir ...
- python_OS 模块
os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...
- python_os.path模块用法
python中os.path模块用法: dirname() 用于去掉文件名,返回目录所在的路径 >>> import os >>> os.path.dirname ...
- 人生苦短_我用Python_OS对目录/文件操作_005
# coding=utf-8 import os # 操作文件和目录 ", os.getcwd()) # 获取当前文件的目录 ", os.path.realpath(__file_ ...
- python_os 的知识点
1. os.getcwd() #获得当前路径 2. os.listdir(path) #列出path路径下的所有目录名和文件名包括后缀 3. os.mkdir(path) #在path创建一个目录 4 ...
- 多测师讲解python_os模块_高级讲师肖sir
#os.path.isfile()#:判断当前是否为文件,返回布尔值是文件则True否者Falsea_path='F:\cms搭建.rar' #lesson包b_path=r'D:\bao\kk '# ...
- python的os模块fnmatch模块介绍
一.先介绍一下os模块 import os print(os.getcwd()) # E:\python\test\python_models # 获取当前的目录 print(os.listdir(& ...
- Python 文件夹及文件操作
import os import os.path from shutil import copy def copyfile(src, dst): count = 1 for filename in o ...
随机推荐
- ACM Same binary weight
Same binary weight 时间限制:300 ms | 内存限制:65535 KB 难度:3 描述 The binary weight of a positive integer ...
- [Java] 利用LinkedHashMap来实现LRU Cache
http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html https://oj.leetcode.com/discuss/1188/java- ...
- 【POJ】2151 Check the difficulty of problems
http://poj.org/problem?id=2151 题意:T个队伍M条题目,给出每个队伍i的每题能ac的概率p[i][j],求所有队伍至少A掉1题且冠军至少A掉N题的概率(T<=100 ...
- Codeforces Beta Round #7
A题,应该是水题,我没看懂... B题,这题很多坑,注意x是LL,而且x = 0的情况,所以初始化要为-1,最后这题是内存管理啊..操作系统学的不好,题意读不懂啊. 申请内存的时候,是从头找 如果这一 ...
- OSG动画学习
OSG动画学习 转自:http://bbs.osgchina.org/forum.php?mod=viewthread&tid=3899&_dsign=2587a6a9 学习动画,看了 ...
- SpringMVC+Shiro权限管理
什么是权限呢?举个简单的例子: 我有一个论坛,注册的用户分为normal用户,manager用户.对论坛的帖子的操作有这些:添加,删除,更新,查看,回复我们规定:normal用户只能:添加,查看,回复 ...
- select()函数 timval问题
如果select调用中设置了等待时间,那么每次调用时都需要重新对这个时间赋值.例如: struct timval tv; while(1) { ........; tv.tv_sec = 2; ...
- [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间
16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进 ...
- [zt] Android中使用List列表
原文地址:http://www.vogella.com/tutorials/AndroidListView/article.html 1. Android and Lists 1.1. Using l ...
- nginx连接php fastcgi配置
匹配到php结尾的文件抛到后端 后端php端口9000