<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 6.5.?與目錄共事 `os.path` 模塊有幾個操作文件和目錄的函數。這里,我們看看如何操作路徑名和列出一個目錄的內容。 ## 例?6.16.?構造路徑名 ``` >>> import os >>> os.path.join("c:\\music\\ap\\", "mahadeva.mp3") 'c:\\music\\ap\\mahadeva.mp3' >>> os.path.join("c:\\music\\ap", "mahadeva.mp3") 'c:\\music\\ap\\mahadeva.mp3' >>> os.path.expanduser("~") 'c:\\Documents and Settings\\mpilgrim\\My Documents' >>> os.path.join(os.path.expanduser("~"), "Python") 'c:\\Documents and Settings\\mpilgrim\\My Documents\\Python' ``` | | | | --- | --- | | \[1\] | `os.path` 是一個模塊的引用;使用哪一個模塊要看你正運行在哪種平臺上。就像 [`getpass`](index.html#crossplatform.example "例?6.2.?支持特定平臺功能") 通過將 `getpass` 設置為一個與平臺相關的函數從而封裝了平臺之間的不同。`os` 通過設置 `path` 封裝不同的相關平臺模塊。 | | \[2\] | `os.path` 的 `join` 函數把一個或多個部分路徑名連接成一個路徑名。在這個簡單的例子中,它只是將字符串進行連接。(請注意在 Windows 下處理路徑名是一個麻煩的事,因為反斜線字符必須被轉義。) | | \[3\] | 在這個幾乎沒有價值的例子中,在將路徑名加到文件名上之前,`join` 將在路徑名后添加額外的反斜線。當發現這一點時我高興極了,因為當用一種新的語言創建我自已的工具包時,`addSlashIfNecessary` 總是我必須要寫的那些愚蠢的小函數之一。在 Python 中_不要_ 寫這樣的愚蠢的小函數,聰明的人已經為你考慮到了。 | | \[4\] | `expanduser` 將對使用 `~` 來表示當前用戶根目錄的路徑名進行擴展。在任何平臺上,只要用戶擁有一個根目錄,它就會有效,像 Windows、UNIX 和 Mac OS X,但在 Mac OS 上無效。 | | \[5\] | 將這些技術組合在一起,你可以容易地為在用戶根目錄下的目錄和文件構造出路徑名。 | ## 例?6.17.?分割路徑名 ``` >>> os.path.split("c:\\music\\ap\\mahadeva.mp3") ('c:\\music\\ap', 'mahadeva.mp3') >>> (filepath, filename) = os.path.split("c:\\music\\ap\\mahadeva.mp3") >>> filepath 'c:\\music\\ap' >>> filename 'mahadeva.mp3' >>> (shortname, extension) = os.path.splitext(filename) >>> shortname 'mahadeva' >>> extension '.mp3' ``` | | | | --- | --- | | \[1\] | `split` 函數對一個全路徑名進行分割,返回一個包含路徑和文件名的 tuple。還記得我說過你可以使用[多變量賦值](../native_data_types/declaring_variables.html#odbchelper.multiassign "3.4.2.?一次賦多值")從一個函數返回多個值嗎?對,`split` 就是這樣一個函數。 | | \[2\] | 我們將 `split` 函數的返回值賦值給一個兩個變量的 tuple。每個變量接收到返回 tuple 相對應的元素值。 | | \[3\] | 第一個變量,`filepath`,接收到從 `split` 返回 tuple 的第一個元素的值,文件路徑。 | | \[4\] | 第二個變量,`filename`,接收到從 `split` 返回 tuple 的第二個元素的值,文件名。 | | \[5\] | `os.path` 也包含了一個 `splitext` 函數,可以用來對文件名進行分割,并且返回一個包含了文件名和文件擴展名的 tuple。我們使用相同的技術來將它們賦值給獨立的變量。 | ## 例?6.18.?列出目錄 ``` >>> os.listdir("c:\\music\\_singles\\") ['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3'] >>> dirname = "c:\\" >>> os.listdir(dirname) ['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'cygwin', 'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'IO.SYS', 'MSDOS.SYS', 'Music', 'NTDETECT.COM', 'ntldr', 'pagefile.sys', 'Program Files', 'Python20', 'RECYCLER', 'System Volume Information', 'TEMP', 'WINNT'] >>> [f for f in os.listdir(dirname) ... if os.path.isfile(os.path.join(dirname, f))] ['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'IO.SYS', 'MSDOS.SYS', 'NTDETECT.COM', 'ntldr', 'pagefile.sys'] >>> [f for f in os.listdir(dirname) ... if os.path.isdir(os.path.join(dirname, f))] ['cygwin', 'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'Music', 'Program Files', 'Python20', 'RECYCLER', 'System Volume Information', 'TEMP', 'WINNT'] ``` | | | | --- | --- | | \[1\] | `listdir` 函數接收一個路徑名,并返回那個目錄的內容的 list。 | | \[2\] | `listdir` 同時返回文件和文件夾,并不指出哪個是文件,哪個是文件夾。 | | \[3\] | 你可以使用[過濾列表](../power_of_introspection/filtering_lists.html "4.5.?過濾列表")和 `os.path` 模塊的 `isfile` 函數,從文件夾中將文件分離出來。`isfile` 接收一個路徑名,如果路徑表示一個文件,則返回 1,否則為 0。在這里,我們使用 ``os.path`.`join`` 來確保得到一個全路徑名,但 `isfile` 對部分路徑 (相對于當前目錄) 也是有效的。你可以使用 `os.getcwd()` 來得到當前目錄。 | | \[4\] | `os.path` 還有一個 `isdir` 函數,當路徑表示一個目錄,則返回 1,否則為 0。你可以使用它來得到一個目錄下的子目錄列表。 | ## 例?6.19.?在 `fileinfo.py` 中列出目錄 ``` def listDirectory(directory, fileExtList): "get list of file info objects for files of particular extensions" fileList = [os.path.normcase(f) for f in os.listdir(directory)] fileList = [os.path.join(directory, f) for f in fileList if os.path.splitext(f)[1] in fileExtList] ``` | | | | --- | --- | | \[1\] | `os.listdir(directory)` 返回在 `directory` 中所有文件和文件夾的一個 list。 | | \[2\] | 使用 `f` 對 list 進行遍歷,我們使用 `os.path.normcase(f)` 根據操作系統的缺省值對大小寫進行標準化處理。`normcase` 是一個有用的函數,用于對大小寫不敏感操作系統的一個補充。這種操作系統認為 `mahadeva.mp3` 和 `mahadeva.MP3` 是同一個文件名。例如,在 Windows 和 Mac OS 下,`normcase` 將把整個文件名轉換為小寫字母;而在 UNIX 兼容的系統下,它將返回未作修改的文件名。 | | \[3\] | 再次用 `f` 對標準化后的 list 進行遍歷,我們使用 `os.path.splitext(f)` 將每個文件名分割為名字和擴展名。 | | \[4\] | 對每個文件,我們查看擴展名是否在我們關心的文件擴展名 list 中 (`fileExtList`,被傳遞給 `listDirectory` 函數)。 | | \[5\] | 對每個我們所關心的文件,我們使用 `os.path.join(directory, f)` 來構造這個文件的全路徑名,接著返回這個全路徑名的 list。 | > 注意 > 只要有可能,你就應該使用在 `os` 和 `os.path` 中的函數進行文件、目錄和路徑的操作。這些模塊是對平臺相關模塊的封裝模塊,所以像 `os.path.split` 這樣的函數可以工作在 UNIX、Windows、Mac OS 和 Python 所支持的任一種平臺上。 還有一種獲得目錄內容的方法。它非常強大,并使用了一些你在命令行上工作時可能已經熟悉的通配符。 ## 例?6.20.?使用 `glob` 列出目錄 ``` >>> os.listdir("c:\\music\\_singles\\") ['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3'] >>> import glob >>> glob.glob('c:\\music\\_singles\\*.mp3') ['c:\\music\\_singles\\a_time_long_forgotten_con.mp3', 'c:\\music\\_singles\\hellraiser.mp3', 'c:\\music\\_singles\\kairo.mp3', 'c:\\music\\_singles\\long_way_home1.mp3', 'c:\\music\\_singles\\sidewinder.mp3', 'c:\\music\\_singles\\spinning.mp3'] >>> glob.glob('c:\\music\\_singles\\s*.mp3') ['c:\\music\\_singles\\sidewinder.mp3', 'c:\\music\\_singles\\spinning.mp3'] >>> glob.glob('c:\\music\\*\\*.mp3') ``` | | | | --- | --- | | \[1\] | 正如你前面看到的,`os.listdir` 簡單地取一個目錄路徑,返回目錄中的所有文件和子目錄。 | | \[2\] | `glob` 模塊,另一方面,接受一個通配符并且返回文件的或目錄的完整路徑與之匹配。這個通配符是一個目錄路徑加上“*.mp3”,它將匹配所有的 `.mp3` 文件。注意返回列表的每一個元素已經包含了文件的完整路徑。 | | \[3\] | 如果你要查找指定目錄中所有以“s”開頭并以“.mp3”結尾的文件,也可以這么做。 | | \[4\] | 現在考查這種情況:你有一個 `music` 目錄,它包含幾個子目錄,子目錄中包含一些 `.mp3` 文件。使用兩個通配符,僅僅調用 `glob` 一次就可以立刻獲得所有這些文件的一個 list。一個通配符是 `"*.mp3"` (用于匹配 `.mp3` 文件),另一個通配符是_子目錄名本身_,用于匹配 `c:\music` 中的所有子目錄。這看上去很簡單,但它蘊含了強大的功能。 | ## 進一步閱讀 * Python Knowledge Base 回答了[關于 `os` 模塊的問題](http://www.faqts.com/knowledge-base/index.phtml/fid/240)。 * _Python Library Reference_ 提供了 [`os`](http://www.python.org/doc/current/lib/module-os.html) 模塊和 [`os.path`](http://www.python.org/doc/current/lib/module-os.path.html) 模塊的文檔。
                  <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>

                              哎呀哎呀视频在线观看