<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                python在安裝的時候,就自帶了很多模塊,我們把這些模塊稱之為標準庫,其中,有一個是使用頻率比較高的,就是 os 。這個庫中方法和屬性眾多,有興趣的看官可以參考官方文檔:[https://docs.python.org/2/library/os.html](https://docs.python.org/2/library/os.html),或者在交互模式中,用`dir(os)`看一看。 ~~~ >>> import os #這個動作很重要,不能缺少 >>> 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)](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/130.md)、[不要紅頭文件(2)](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/131.md)) 關于`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) Split a pathname. Returns tuple "(head, tail)" where "tail" is everything after the final slash. Either part may be empty. ~~~ 以下將一些典型舉例說明: 特別說明,下面的所有操作,均是進入到如下的目錄中進行的。 ~~~ qw@qw-Latitude-E4300:~/Documents/ITArticles/BasicPython/codes$ pwd /home/qw/Documents/ITArticles/BasicPython/codes #當前目錄 qw@qw-Latitude-E4300:~/Documents/ITArticles/BasicPython/codes$ python Python 2.7.6 (default, Nov 13 2013, 19:24:16) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ~~~ ## 文件的絕對路徑 ~~~ >>> import os.path >>> os.path.abspath("225.py") '/home/qw/Documents/ITArticles/BasicPython/codes/225.py' ~~~ 文件 225.py 是真實存在上述路徑中的,得到了該文件的絕對路徑。但是,如果隨便提供一個不在這個目錄中的文件,又如何? ~~~ >>> os.path.isfile("225.py") True >>> os.path.isfile("2222.py") False >>> os.path.abspath("2222.py") '/home/qw/Documents/ITArticles/BasicPython/codes/2222.py' ~~~ `os.path.isfile(path)`,可以判斷path中是否是文件,其實是判斷在該路徑中,是否存在那個文件,如果存在則返回True,否則False。上面的操作發現 2222.py 這個文件在當前目錄下是不存在的,但是,用`os.path.abspaht("2222.py")`能夠返回一個絕對路徑并帶有這個不存在的文件的文件名。這里不妨理解為,如果要建立這個文件,它即將被放在那個位置。 按照這樣理解,還可: ~~~ >>> os.path.abspath("/home/qw/kkkkkkkk.kk") '/home/qw/kkkkkkkk.kk' ~~~ ## [](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/226.md#分開目錄和文件名)分開目錄和文件名 ~~~ >>> pn = os.path.abspath("225.py") >>> pn '/home/qw/Documents/ITArticles/BasicPython/codes/225.py' >>> os.path.split(pn) ('/home/qw/Documents/ITArticles/BasicPython/codes', '225.py') >>> path, filename = os.path.split(pn)[0], os.path.split(pn)[1] >>> path '/home/qw/Documents/ITArticles/BasicPython/codes' >>> filename '225.py' ~~~ `os.paht.split()`,參數是目錄加文件名,就可以將路徑和文件名分開。其實,我看這個功能不是很智能,你看這樣 ~~~ >>> os.path.split("/home/qw") ('/home', 'qw') >>> os.path.split("/home/qw/") ('/home/qw', '') ~~~ 它就是將最后一組認為是文件名了,即最后一個`/`后面的就是文件名,所以第二個實驗中,文件名是空了。是不是有點傻呢? 同樣,參數中的文件或者目錄,不一定是你的電腦中真實存在的,請看: ~~~ >>> os.path.split("/foo/python/qiwsir/git.git") ('/foo/python/qiwsir', 'git.git') ~~~ 只要符合目錄書寫結構,就可以分解了。 有另外兩個屬性,是`os.path.split()`的分別執行,即可以分別獲得路徑和文件名,這樣讓操作更簡單了。 ~~~ >>> os.path.dirname("/foo/python/qiwsir/git.git") '/foo/python/qiwsir' >>> os.path.basename("foo/python/qiwsir/git.git") 'git.git' ~~~ ## 判斷 前面稍微提到了`os.path.isfile()`可以用來判斷一個文件是否存在,那么判斷目錄路徑是否存在,可否?可: ~~~ >>> os.path.exists("/foo/python/qiwsir") False >>> os.path.exists("/home/qw/Documents") True ~~~ 判斷相關的屬性還有: * `os.path.isabs(path)`:判斷path是否為絕對路徑 * `os.paht.isdir(path)`:判斷path是否為存在的目錄 ## 組合路徑 將兩個或多個對象組合起來,是常見的事情,那么如何將多個路徑組合呢?如下: ~~~ >>> os.path.join("/home/python","/BasicsPython","226.md") '/BasicsPython/226.md' ~~~ 特別提醒,這個屬性的返回值中,將第一個絕對路徑忽略。 ~~~ >>> os.path.join("/","/home/qw","learnpython.md") '/home/qw/learnpython.md' ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看