博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python os
阅读量:4221 次
发布时间:2019-05-26

本文共 1268 字,大约阅读时间需要 4 分钟。

os.getcwd() os.getcwdb()

>>> os.getcwd()'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35'>>> os.getcwdb()b'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35'

os.listdir(path='.')

>>> os.listdir(os.getcwd())['ABBREV.txt', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python35.dll', 'pythonw.exe', 'README.txt', 'Scripts', 'sd.db', 'somefile.txt', 'tcl', 'Tools', 'vcruntime140.dll', 'vv.txt']
返回所有文件和文件夹。顺序不定。如果path是bytes,返回的也是bytes。

os.mkdir(path, mode=0o777, *, dir_fd=None)    os.makedirs(name, mode=0o777, exist_ok=False)

import osos.mkdir('image')try:    os.mkdir('download/image')except OSError as why:    print("Fail: %s" % str(why))
Fail: [WinError 3] 系统找不到指定的路径。: 'download/image'
路径存在可以:

import os#os.mkdir('image') 已存在try:    os.mkdir('image/good')except OSError as why:    print("Fail: %s" % str(why))
makedirs:路径所有都可以创建

import ostry:    os.makedirs('image/good/better')except OSError as why:    print("Fail: %s" % str(why))

os.rmdir(path, *, dir_fd=None)

被删除目录一定要为空:

import ostry:    os.rmdir('image/good/better')except OSError as why:    print("Fail: %s" % str(why))try:    os.rmdir('image')except OSError as why:    print("Fail: %s" % str(why))
Fail: [WinError 145] 目录不是空的。: 'image'

转载地址:http://eqmmi.baihongyu.com/

你可能感兴趣的文章
Host 'XXX' is not allowed to connect to this MySQL server解决方案
查看>>
corosync pacemaker 配置高可用集群(一)
查看>>
5种IO模型、阻塞IO和非阻塞IO、同步IO和异步IO
查看>>
nginx(一) nginx详解
查看>>
nginx(二) nginx编译安装 及 配置WEB服务
查看>>
nginx(三) nginx配置:反向代理 负载均衡 后端健康检查 缓存
查看>>
nginx(四) nginx+keepalived 实现主备+双主热备模型的高可用负载均衡代理服务
查看>>
jQuery核心--多库共存
查看>>
QTP Tutorial #23 – QTP Smart Object Identification, Sync Point, and Test Result Analysis
查看>>
第一章漫话自动化测试
查看>>
第二章:Selenium IDE应用实践
查看>>
第三章:Python基础
查看>>
正则表达式
查看>>
第五章 自动化测试模型
查看>>
Linux命令行与shell编程第3章基本的shell
查看>>
Linux命令行与shell编程第4章 更多的bash shell命令
查看>>
4 51 单片机最小系统
查看>>
6 51点亮第一个LED
查看>>
8 51 LED流水灯
查看>>
Multisim 14.0 搭建并仿真51单片机最小系统
查看>>