python 判斷是檔案還是目錄

2022-02-09 06:41:36 字數 1785 閱讀 6031

1樓:ilove爆頭

look~~

>>> os.path.exists("te")true

>>> os.path.exists("nothing")false

>>> os.path.isfile("nothing")false

>>> os.path.isdir("nothing")false

>>>>>> os.path.isdir("te")false

>>> os.path.isfile("te")true

>>>建議你先判斷是否存在,如果確實存在,你再進行判斷是檔案還是資料夾linux,資料夾名和同級目錄的檔名是不可以同時存在的。

zhangzhipeng@earth:~$ mkdir temkdir: cannot create directory `te': file exists

zhangzhipeng@earth:~$ rm tezhangzhipeng@earth:~$ mkdir tezhangzhipeng@earth:

~$ > te-bash: te: is a directorywindows中:

可以證明,不管windows還是linux,同級目錄下,是不允許出現同名檔案(夾)的

但是這不能代表不是檔案就是資料夾啊,首先得確認這個檔案(夾)是存在的。

其實,資料夾也是一個檔案。

2樓:匿名使用者

os.listdir("路徑")列出所有檔案和目錄

os.path.isdir(「檔名」)判斷是目錄嗎,是返回true,不是返回false

os.path.isfile同上判斷檔案

3樓:

os模組下

os.path.isdir(path)

這個函式判斷是否是目錄試試卡

python如何判斷一個目錄下是否存在某個檔案?

4樓:匿名使用者

1.使用os模組用os模組中os.path.exists()方法檢測是否存在test_file.txt檔案

import os

os.path.exists(test_file.txt)#true

os.path.exists(no_exist_file.txt)#false

2.使用try命令使用open()方法,如果要開啟的檔案不存在,就回跑出異常,用try()方法捕獲異常。

try:

f =open(test_file.txt)f.close()

except ioerror:

print "file is not accessible"

3. 使用pathlib檢查路徑是否存在

path = pathlib.path("path/file")path.exist()

檢查路徑是否是檔案

path = pathlib.path("path/file")path.is_file()

python如何判斷一個目錄下是否存在某個檔案??謝啦!

5樓:波盡天下

使用os.path.exists()方法可以直接判斷檔案是否存在。

**如下:

>>> import os

>>> os.path.exists(r'c:\1.txt')false

>>>如果存在返回值為true如果不存在則返回false。很方便希望對你有所幫助~~

python中怎樣將檔案拷貝到指定的目錄下

import shutil shutil.copy sourcedir,targetdir 好像不能直接import os搞定,得import shutil import shutil 複製檔案 shutil.copyfile listfile.py d test.py 複製目錄 shutil.co...

Creo3 0材料目錄是哪個檔案

你好,creo的模板所在資料夾的位置 要根據自己安裝的路徑來定 每個人的路徑不一樣 如下是我電腦中creo的template路徑,供你參考,你可以試著找找看,應該沒問題的。creo工程圖 繪圖設定目錄 在 1 畫完工程圖後,把 繪圖屬性 裡的設定儲存下來,一般會儲存在當前的工作目錄下,想要刪掉。直接...

怎麼判斷php是ts還是nts,怎麼判斷PHP是TS還是NTS?

通過phpinfo 檢視其中的 thread safety 項,這個專案就是檢視是否是執行緒安全,如果是 enabled,一般來說應該是ts版,否則是nts版。如何判斷php 是ts還是nts版的?如何判斷php 是ts還是nts版的 通過phpinfo 檢視其中的 thread safety 項,...