常用方法

os.listdir(path)

返回指定的文件夹包含的文件或文件夹的路径的列表(list),其中作为参数的path必须是绝对目录

os.system(‘’)

执行外部shell命令。
image.png

汇总

方法 功能
os.access(path, name) 特权模式
os.chdir(path) 改变当前工作目录
os.chflags(path, flags) 设置路径的标记为数字标记
os.chmod(path, name) 更改权限
os.chown(path, uid, gid) 更改文件所有者
os.chroot(path) 改变当前进程的根目录os.close(x)
os.close(x) 关闭文件
os.fchdir(x) 通过文件描述符改变当前工作目录
os.getcwd() 返回当前工作目录
os.listdir(path) 返回当前目录下的所有文件及文件夹的列表
os.mkdir(path[,name]) 创建一个名字为name的文件夹名
os.open(file) 打开一个文件
os.remove(path) 删除path路径的文件
os.rename(src,dst) 修改文件名,src为源文件,dst为目标文件
os.rmdir(path) 删除path对应的目录

os.path

os.path.dirname

获得目录的路径名称(不含文件名)

os.path.isfile(path)

判断路径是否为文件

os.path.splitext(file)

分离文件名与扩展名
os.path.splitext(file)[0] ——获得文件名
os.path.splitext(file)[1] ——获得文件扩展名

汇总

os.path.abspath(path) 返回路径名路径的规范化绝对化版本
os.path.basename(path) 返回路径名路径的基名称
os.path.commonprefix(list) 返回序列路径中每个路径名的最长公共子路径
os.path.dirname(path) 返回路径名路径的目录名
os.path.exists(path) 如果路径引用现有路径或打开的文件描述符,则返回True,否则返回Flase
os.path.lexists 如果路径引用现有路径,则返回True
os.path.expanduser(path) 返回参数,初始组件为~或~user替换为该用户的主目录
os.path.expandvars(path) 返回环境变量展开的参数
os.path.getatime(path) 返回路径的最后访问时间
os.path.getmtime(path) 返回上次修改路径的时间
os.path.getctime(path) 返回文件 path 的创建时间
os.path.getsize(path) 返回路径的大小,以字节为单位
os.path.isabs(path) 如果路径是绝对路径名,则返回True
os.path.isfile(path) 如果路径是文件,则返回True
os.path.isdir(path) 如果路径是目录,则返回True
os.path.islink(path) 如果路径是链接,则返回True
os.path.ismount(path) 如果路径是挂载点,则返回True
os.path.join(path1[, path2[, …]]) 把目录和文件名合成一个路径
os.path.normcase(path) 转换path的大小写和斜杠
os.path.normpath(path) 规范path字符串形式
os.path.realpath(path) 返回path的真实路径
os.path.relpath(path[, start]) 计算相对路径
os.path.samefile(path1, path2) 判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2) 判断fp1和fp2是否指向同一文件