Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

os模块汇总

$
0
0

os.getcwd()

函数得到当前python脚本工作的目录路径。

os.listdir('path')

返回指定目录下的所有文件和目录名。

os.listdir(os.getcwd()) ['jquery-1.12.4.min.js', 'phantomjs-2.1.1-linux-x86_64', 'scrapy-redis', 'area_facility.sql', 'Shen Ru Li Jie Ji Suan Ji Xi Tong Di Er Ban - Wei Zhi.azw3', 'paixu.py', 'diandai.py', 'Sina', 'you-get', 'my_blog', 'cgi-bin'] View Code

os.chdir('path')

修改当前工作目录。

os.remove()

删除一个文件。

os.system()

运行shell命令。

os.chmod('path','mode')

修改文件权限。

os.stat('path')

1 os.stat('/home/python/Desktop/') 2 posix.stat_result(st_mode=16877, st_ino=525064, st_dev=2049, st_nlink=8, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1482919247, st_mtime=1482919013, st_ctime=1482919013) 它返回一个类元组对象(stat_result 对象, 包含 10 个元素), 依次是 st_mode (权限模式), st_ino (inode number), st_dev (device), st_nlink (number of hardlinks), st_uid (所有者用户 ID), st_gid (所有者所在组 ID ), st_size (文件大小, 字节), st_atime (最近一次访问时间), st_mtime (最近修改时间),st_ctime (平台相关; Unix 下的最近一次元数据/metadata 修改时间, 或者windows 下的创建时间)

os.path.split('path+file')

函数返回一个路径的目录名和文件名。

1 os.path.split('/home/python/Desktop/jquery-1.12.4.min.js') 2 ('/home/python/Desktop', 'jquery-1.12.4.min.js')

os.path.isfile()和os.path.isdir()

函数分别检验给出的路径是一个文件还是目录。

1 os.path.isfile('jquery-1.12.4.min.js') 2 True 3 os.path.isdir('you-get/') 4 True

os.path.abspath(name)

获得绝对路径。

os.path.getsize(name)

获得文件大小,单位是b,如果name是目录返回0L

os.path.splitext('file')

分离文件名与扩展名

1 os.path.splitext('jquery-1.12.4.min.js') 2 ('jquery-1.12.4.min', '.js') os.path.join(path,name)
连接目录与文件名或目录 1 os.path.join('/home/python/Desktop/','jquery-1.12.4.min.js') 2 '/home/python/Desktop/jquery-1.12.4.min.js'

Viewing all articles
Browse latest Browse all 9596

Trending Articles