首页 > os.mkdir() 和 os.makedirs() 的区别

os.mkdir() 和 os.makedirs() 的区别

用Python做的 爬虫:

为什么第一个没有报错?

thx in advance


os.mkdir 與 os.makedirs 的差別在於 os.makedirs 會遞迴地去建立目錄,也就是說連同中繼的目錄也會一起建立,就類似於 Linux 中的 mkdir -p

>>> import os
>>> os.mkdir('foo/bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: 'foo/bar'
>>> os.makedirs('foo/bar')

使用 os.mkdir 時,如果你給定的 path 參數是個多層的 path,如果某個中繼的目錄不存在(比如說上例中的 foo), Python 將會報錯.

但如果使用 os.makedirs 則 Python 會連同中間的目錄一起建立.但有一點值得注意,當 path 末端的目錄已經存在的話,os.makedirs 也是會引發例外.


我想你的問題就在這裡,你可以檢查 home 目錄一開始是否存在.

要注意的是,這邊的路徑認定是要看你啟動 Python 直譯器的地方,也就是說你要確定你運行 python 所在的目錄下面要有 home 才能避免 os.mkdir 出錯.

【热门文章】
【热门文章】