本文共 1268 字,大约阅读时间需要 4 分钟。
>>> os.getcwd()'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35'>>> os.getcwdb()b'D:\\Users\\dyan\\AppData\\Local\\Programs\\Python\\Python35'
>>> 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。
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))
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/