Python模块简介--os模块

一、模块简介
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)

你可能感兴趣的:(Python模块简介--os模块)