您的位置:首页 > 编程语言 > Python开发

Python模块简介--os模块

2018-01-18 14:56 225 查看
一、模块简介

Python os模块包含普遍的操作系统功能,让你的程序和平台分开。通常用于返回和打开指定目录下的所有文件和目录名。

二、常用函数

1、os.listdir(path) —– 返回指定目录下的所有文件和目录名

folder = 'rt_html'
for movie_html in os.listdir(folder)


os.listdir(folder_name)---返回路径下的文件名


2、os.path.join(path,name) —- 链接目录和文件名

folder = 'rt_html'
for movie_html in os.listdir(folder):
with open (os.path.join(folder,movie_html)) as file:


3、os.path.exists() —- 函数用来检验给出的路径是否真地存在

os.path.exists(‘C:\Python25\abc.txt’)
False
os.path.exists(‘C:\Python25’)
True


4、os.makedirs() —- 用于创建目录

folder_name = 'ebert_reviews'
if not os.path.exists(folder_name):
os.makedirs(folder_name)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pythonos