跟老齐学Python之折腾一下目录
python在安装的时候,就自带了很多模块,我们把这些模块称之为标准库,其中,有一个是使用频率比较高的,就是os。这个库中方法和属性众多,有兴趣的看官可以参考官方文档:https://docs.python.org/2/library/os.html,或者在交互模式中,用dir(os)看一看。
>>>importos #这个动作很重要,不能缺少 >>>dir(os) ['EX_CANTCREAT','EX_CONFIG','EX_DATAERR','EX_IOERR','EX_NOHOST','EX_NOINPUT','EX_NOPERM','EX_NOUSER','EX_OK','EX_OSERR','EX_OSFILE','EX_PROTOCOL','EX_SOFTWARE','EX_TEMPFAIL','EX_UNAVAILABLE','EX_USAGE','F_OK','NGROUPS_MAX','O_APPEND','O_ASYNC','O_CREAT','O_DIRECT','O_DIRECTORY','O_DSYNC','O_EXCL','O_LARGEFILE','O_NDELAY','O_NOATIME','O_NOCTTY','O_NOFOLLOW','O_NONBLOCK','O_RDONLY','O_RDWR','O_RSYNC','O_SYNC','O_TRUNC','O_WRONLY','P_NOWAIT','P_NOWAITO','P_WAIT','R_OK','SEEK_CUR','SEEK_END','SEEK_SET','TMP_MAX','UserDict','WCONTINUED','WCOREDUMP','WEXITSTATUS','WIFCONTINUED','WIFEXITED','WIFSIGNALED','WIFSTOPPED','WNOHANG','WSTOPSIG','WTERMSIG','WUNTRACED','W_OK','X_OK','_Environ','__all__','__builtins__','__doc__','__file__','__name__','__package__','_copy_reg','_execvpe','_exists','_exit','_get_exports_list','_make_stat_result','_make_statvfs_result','_pickle_stat_result','_pickle_statvfs_result','_spawnvef','abort','access','altsep','chdir','chmod','chown','chroot','close','closerange','confstr','confstr_names','ctermid','curdir','defpath','devnull','dup','dup2','environ','errno','error','execl','execle','execlp','execlpe','execv','execve','execvp','execvpe','extsep','fchdir','fchmod','fchown','fdatasync','fdopen','fork','forkpty','fpathconf','fstat','fstatvfs','fsync','ftruncate','getcwd','getcwdu','getegid','getenv','geteuid','getgid','getgroups','getloadavg','getlogin','getpgid','getpgrp','getpid','getppid','getresgid','getresuid','getsid','getuid','initgroups','isatty','kill','killpg','lchown','linesep','link','listdir','lseek','lstat','major','makedev','makedirs','minor','mkdir','mkfifo','mknod','name','nice','open','openpty','pardir','path','pathconf','pathconf_names','pathsep','pipe','popen','popen2','popen3','popen4','putenv','read','readlink','remove','removedirs','rename','renames','rmdir','sep','setegid','seteuid','setgid','setgroups','setpgid','setpgrp','setregid','setresgid','setresuid','setreuid','setsid','setuid','spawnl','spawnle','spawnlp','spawnlpe','spawnv','spawnve','spawnvp','spawnvpe','stat','stat_float_times','stat_result','statvfs','statvfs_result','strerror','symlink','sys','sysconf','sysconf_names','system','tcgetpgrp','tcsetpgrp','tempnam','times','tmpfile','tmpnam','ttyname','umask','uname','unlink','unsetenv','urandom','utime','wait','wait3','wait4','waitpid','walk','write']
在这么多的东西中,本讲只关注os.path,真所谓“弱水三千,只取一瓢”,为什么这么偏爱它呢?因为它和前面已经讲过的文件操作进行配合,就能够随心所欲操作各个地方的文件了(关于文件,请参考:不要红头文件(1)、不要红头文件(2))
关于os.path的属性也不少,依然可以用dir(os.path)查看:
>>>dir(os.path) ['__all__','__builtins__','__doc__','__file__','__name__','__package__','_joinrealpath','_unicode','_varprog','abspath','altsep','basename','commonprefix','curdir','defpath','devnull','dirname','exists','expanduser','expandvars','extsep','genericpath','getatime','getctime','getmtime','getsize','isabs','isdir','isfile','islink','ismount','join','lexists','normcase','normpath','os','pardir','pathsep','realpath','relpath','samefile','sameopenfile','samestat','sep','split','splitdrive','splitext','stat','supports_unicode_filenames','sys','walk','warnings']
这么多属性,看官可以用help()逐个查看有关信息,并了解其使用方法。下面列出常见的几个使用方法,为看官减轻一点阅读英文的障碍,不过,如果看官英语足够好,请直接看原文档。就像这样:
>>>help(os.path.split)
split(p) Splitapathname. Returnstuple"(head,tail)"where"tail"is everythingafterthefinalslash. Eitherpartmaybeempty.