3 回答

TA貢獻1836條經(jīng)驗 獲得超5個贊
在os.path模塊中有個isfile的方法,該方法可以判斷是不是文件,返回True說明是文件,返回False則不是文件,下面的英文是摘自python文檔
os.path.isfile(path)
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
用法也很簡單
1 2 3 | import os filename='/tmp/test.txt' print os.path.isfile(filename) |

TA貢獻1719條經(jīng)驗 獲得超6個贊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> import os.path >>> help(os.path.isfile) Help on function isfile in module genericpath:
isfile(path) Test whether a path is a regular file
>>> help(os.path.isdir) Help on function isdir in module genericpath:
isdir(s) Return true if the pathname refers to an existing directory.
>>> os.path.isfile('/etc/hostname') True >>> os.path.isdir('/etc/hostname') False >>> |
兩個函數(shù)都在os.path模塊里面。參見上文。
添加回答
舉報