python模块os
一、os模块概述
Python os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。(一语中的)
二、常用方法
1、os.name
输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。
2、os.getcwd()
函数得到当前工作目录,即当前Python脚本工作的目录路径。
#!/bin/env python
#-*- encoding=utf8 -*-
import os if __name__=="__main__":
# 函数得到当前工作目录,即当前Python脚本工作的目录路径。
print os.getcwd() 执行命令: D:\python_test>python test_path.py
输出结果: D:\python_test 执行命令: D:\>python ./python_test/test_path.py
输出结果: D:
3、os.listdir()
返回指定目录下的所有文件和目录名。
#!/bin/env python
#-*- encoding=utf8 -*-
import os if __name__=="__main__":
# 函数得到当前工作目录,即当前Python脚本工作的目录路径。
cwd = os.getcwd() # 返回指定目录下的所有文件和目录名。
print os.listdir(cwd)
4、os.remove()
删除一个文件。
5、os.system()
运行shell命令。
os.system("cmd") # 启动dos窗口
6、os.sep 可以取代操作系统特定的路径分割符。
7、os.linesep字符串给出当前平台使用的行终止符
>>> import os
>>> os.sep
'\\'
>>> os.linesep
'\r\n'
>>>
8、os.path.split()
函数返回一个路径的目录名和文件名
>>> os.path.split('C:\\Python25\\abc.txt')
('C:\\Python25', 'abc.txt')
9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False
>>> os.path.isfile('D:/python_test/test_path.py')
True
10、os.path.exists()函数用来检验给出的路径是否真地存在
>>> os.path.exists('C:\\Python25\\abc.txt')
False
>>> os.path.exists('C:\\Python25')
True
>>>
11、os.path.abspath(name):获得绝对路径
12、os.path.normpath(path):规范path字符串形式
13、os.path.getsize(name):获得文件大小,如果name是目录返回0L
14、os.path.splitext():分离文件名与扩展名
>>> os.path.splitext('a.txt')
('a', '.txt')
15、os.path.join(path,name):连接目录与文件名或目录
>>> os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'
>>> os.path.join('c:\\Python','f1')
'c:\\Python\\f1'
>>>
16、os.path.basename(path):返回文件名
>>> os.path.basename('a.txt')
'a.txt'
>>> os.path.basename('c:\\Python\\a.txt')
'a.txt'
>>>
17、os.path.dirname(path):返回文件路径
>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'
18、os.chdir('c:\\Python\\a.txt')改变当前路径
os.chdir('D:\Program Files')
19、os.path.dirname(os.path.realpath(__file__))获取当前的__file__的路径
__file__是执行的文件
#!/bin/env python
#-*- encoding=utf8 -*- import os if __name__=="__main__": print __file__ print os.path.realpath(__file__) print os.path.dirname(os.path.realpath(__file__)) 输出结果:
D:\python_test>python test_path.py
test_path.py
D:\python_test\test_path.py
D:\python_test
python模块os的更多相关文章
- python模块-OS模块详解
1.按字母分 os相关的函数:143个.按字母排序如下: ['abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'clo ...
- python模块 os&sys&subprocess&hashlib模块
os模块 # os模块可根据带不带path分为两类 # 不带path print(os.getcwd()) # 得到当前工作目录 print(os.name) # 指定你正在使用的操作系统,windo ...
- python 模块——os 模块
os 模块--操作系统的各种接口 常用函数: os.path os.path.join(path,*paths) (常用,设置文件路径)将一个或者多个路径连接起来. PATH_TO_TEST_IMAG ...
- 024.Python模块OS模块
一 OS模块 对系统进行操作 1.1 popen 可以把运行的结果,这个字符串转化成utf-8这样的编码格式在进行输出 import os res = os.popen("ifconfig& ...
- python模块(os)
os模块 os模块提供了许多与操作系统交互的接口 os.getcwd() -> str # 返回当前路径, 相当于pwd os.chdir("dirname") -> ...
- Python模块 - os , sys.shutil
os 模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录: ...
- python模块--os模块
打印文件的绝对路径:os.path.abspath(__file__) os.path.dirname("/root/python/test.py") #只取目录名'/root ...
- Python模块 os和sys
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...
- python模块--os模块、sys模块
一.os模块 1 os.getcwd() 获取当前工作的目录,即当前python脚本工作的目录路径 2 3 os.chdir("dirname") 改变当前脚本的工作目录:相当于s ...
随机推荐
- Android定位服务关闭和定位(悬浮)等权限拒绝的判断
public void checkLocationPermission() { if (!PermissionHelper.isLocServiceEnable(this)) {//检测是否开启定位服 ...
- Mybatis 系列5-结合源码解析TypeHandler
[Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...
- CRM 2016 IFrame_A嵌入 EXT.net 页面 a.aspx,刷新另一IFrame_B嵌入 b.aspx gird.
说白了就是一个IFrame页面,执行另一IFrame页面的函数. a.aspx JS: parent.Xrm.Page.getControl("IFRAME_B").getObj ...
- Http跨域
一.传统 ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jQuery的json ...
- c#数组去重
第一种: string[] stringArray = { "aaa", "bbb", "aaa", "ccc", &q ...
- BOM跟DOM的区别和关联
BOM 1. BOM是Browser Object Model的缩写,即浏览器对象模型. 2. BOM没有相关标准. 3. BOM的最根本对象是window. 从1可以看出来:BOM和浏览器关系密切. ...
- 搜狗输入法在Ubuntu下突然不能输入中文
使用搜狗输入法在Ubuntu下突然不能输入中文,这种情况出现多次,先找到一个快捷的解决方案: 首先要知道遇到这种情况的很大可能原因是bin目录里文件过多,系统加载不完全,你就算上面的脚本执行3次都不行 ...
- JAVA List合并集合
import java.util.ArrayList; import java.util.List; public class test { public static void main(Strin ...
- TessorFlow学习 之 序言
2017.10.23日记录: 感觉平时自己学的还不错的,可是面试的时候才发现自己是个渣渣~~,真的感觉学习不能闭门造车! 面试了图像处理算法工程师-->> 1.精通哪门语言?C.C++.P ...
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...