Python os模块

来源:互联网 发布:安卓超牛数据恢复软件 编辑:程序博客网 时间:2024/06/17 16:24




>>> import os
>>> dir(os)['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']>>> os.getcwd()    # 获取当前路径'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36'>>> os.chdir("C:\\Users")    # 重置当前路径>>> os.getcwd()'C:\\Users'                  # 重置结果>>> os.chdir(r"C:\Users\Administrator\AppData\Local\Programs\Python\Python36")>>> os.getcwd()'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36'>>> os.curdir    # 当前目录表示'.'>>> os.pardir    # 上一级目录表示'..'>>> os.makedirs(r"C:\Users\Administrator\Desktop\haha")    #  按照路径依次生成,直到创建文件(整个路径也被创建)>>> os.removedirs(r"C:\Users\Administrator\Desktop\haha")    #  按照路径依次删除,直到整个路径(包括文件)被删除>>> os.mkdir(r"C:\Users\Administrator\Desktop\haha\hehe")     #  按照路径去创建文件,路径错误则报错Traceback (most recent call last):  File "<pyshell#14>", line 1, in <module>    os.mkdir(r"C:\Users\Administrator\Desktop\haha\hehe")FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'C:\\Users\\Administrator\\Desktop\\haha\\hehe'>>> os.mkdir(r"C:\Users\Administrator\Desktop\haha")        # 路径正确不报错          
>>> os.rmdir(r"C:\Users\Administrator\Desktop\haha\hehe")     # 按照路径寻找文件并删除,不存在,报错Traceback (most recent call last):  File "<pyshell#16>", line 1, in <module>    os.rmdir(r"C:\Users\Administrator\Desktop\haha\hehe")FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'C:\\Users\\Administrator\\Desktop\\haha\\hehe'>>> os.rmdir(r"C:\Users\Administrator\Desktop\haha")>>> os.listdir(".")     # 获得当前路径的文件列表['3.py', '3级菜单.py', 'credit_card.py', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'log', 'NEWS.txt', 'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'README.txt', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']>>> os.listdir(r"C:\Users\Administrator\Desktop")    # 获得指定路径的文件列表['ATM', 'Chrome.lnk', 'css.chm', 'Lantern.lnk', 'Learn HTML', 'Learn Java Script', 'learnPython', 'R in active', 'random.py', 'userdata.data', '宽带连接.lnk', '数据类.py', '新建文件夹', '有道词典.lnk', '核心练习题', '特殊方法与运算符重载.txt', '生成器.py', '用户注册.py', '百度网盘.lnk', '简历', '金额转换类.py']>>> os.stat(r"C:\Users\Administrator\Desktop\金额转换类.py")     # 获得指定文件的属性os.stat_result(st_mode=33206, st_ino=10696049115027638, st_dev=3362048803, st_nlink=1, st_uid=0, st_gid=0, st_size=2534, st_atime=1497104715, st_mtime=1497177083, st_ctime=1497104715)>>

>>> os.sep   # 获得在代码中 本系统路径分隔符的表示方式'\\'>>> os.linesep  #  换行符'\r\n'>>> os.environ   # 系统环境变量environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\Administrator\\AppData\\Roaming', 'COM.ADOBE.VERSIONCUE.CLIENT.APPLOCALE': 'zh_CN', 'COM.ADOBE.VERSIONCUE.CLIENT.APPNAME': 'AdobeDrive', 'COM.ADOBE.VERSIONCUE.CLIENT.APPVERSION': '1.0.0', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'WIN-FSA3JAE3DN0', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'DEVMGR_SHOW_DETAILS': '1', 'DEVMGR_SHOW_NONPRESENT_DEVICES': '1', 'FP_NO_HOST_CHECK': 'NO', 'HOME': 'C:\\Users\\Administrator', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\Administrator', 'LOCALAPPDATA': 'C:\\Users\\Administrator\\AppData\\Local', 'LOGONSERVER': '\\\\WIN-FSA3JAE3DN0', 'MOZ_PLUGIN_PATH': 'C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\plugins\\', 'NUMBER_OF_PROCESSORS': '8', 'OS': 'Windows_NT', 'PATH': 'C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Common Files\\microsoft shared\\TigerKin;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;%Tiger Kin%;C:\\Program Files (x86)\\Common Files\\microsoft shared\\TigerKin;C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 60 Stepping 3, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '3c03', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PSMODULEPATH': 'C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\', 'PUBLIC': 'C:\\Users\\Public', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\Windows', 'TEMP': 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp', 'TIGERKIN': 'C:\\Program Files (x86)\\Common Files\\microsoft shared\\TigerKin', 'TMP': 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp', 'USERDOMAIN': 'WIN-FSA3JAE3DN0', 'USERNAME': 'Administrator', 'USERPROFILE': 'C:\\Users\\Administrator', 'WINDIR': 'C:\\Windows', 'WINDOWS_TRACING_FLAGS': '3', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log'})>>> os.name  # 系统名字'nt'>>> os.path.abspath('__fille__')  # 获得当前所在的绝对路径'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\__fille__'>>> os.path.split(r"C:\a\b\c\haha.txt")   # 分割路径 与 文件名('C:\\a\\b\\c', 'haha.txt')>>> os.path.dirname(r"C:\a\b\c\haha.txt")   #  获得路径的上一级路径目录'C:\\a\\b\\c'>>> os.path.basename(r"C:\a\b\c\haha.txt")    # 获得路径下的文件名'haha.txt'>>> os.path.exists(r"C:\a\b\c\haha.txt")      #  判断路径是否存在False>>> os.path.isabs(r"C:\a\b\c\haha.txt")       # 判断是否是绝对路径True>>> os.path.isabs(r"\a\b\c\haha.txt")         #True>>> os.path.isabs(r"a\b\c\haha.txt")False>>> os.path.isfile(r"C:\a\b\c\haha.txt")      # 判断是否是文件False>>> os.join.join(


原创粉丝点击