㈠ python 怎麼判斷文件夾中是否有zip
defhaszipfile(targetDir):
forfileinos.listdir(targetDir):
targetFile=os.path.join(targetDir,file)
ifos.path.isfile(targetFile)andtargetFile[-4:].lower=='.zip':
returnTrue
returnFalse
用這個函數就可以
㈡ python判斷是文件還是目錄
若手機無法接到對方電話,但可以撥打電話,建議您:
1.取出SIM卡後重新插入。
2.部分型號支持更改網路模式,建議將網路模式更改為2G嘗試。
3.更換位置試一下。
4.檢查手機是否設置了黑名單(拒絕電話)功能。若有設置,請取消後測試。
5.檢查機器是否設置了呼叫轉移/呼叫限制功能。
6.若手機中有安裝安全衛士方面的軟體,建議卸載刪除。
7.若無效,備份手機中數據(聯系人,簡訊,圖片等),然後恢復出廠設置。
若問題依然存在,請您攜帶購機發票、包修卡和機器送到三星服務中心,由專業的售後工程師幫助檢測。
㈢ python判斷是文件還是目錄的注意事項
#-*-coding:utf-8-*-
importos,re
defstatCodeLines(path,file):
ifre.match(r'.*py$',file):
lines_blank=0
lines_comment=0
lines_total=0
f=open(path+'\'+file,'r')
lines=f.readlines()
lines_total=len(lines)
forlineinlines:
pattern_blank=re.compile(r's*$')
pattern_comment=re.compile(r's*#')
ifpattern_blank.match(line):
lines_blank+=1
ifpattern_comment.match(line):
lines_comment+=1
f.close()
print(lines_total,lines_comment,lines_blank)
returnTrue
else:
pass
defgetFiles(path):
filelist=os.listdir(path)
forfileinfilelist:
fpath=path+'\'+file
#做判斷時需要傳入完整文件路徑
if(os.path.isfile(fpath)):
statCodeLines(path,file)
if(os.path.isdir(fpath)):
getFiles(fpath)
if__name__=='__main__':
#statCodeLines('D:\Documents\VisualStudio2015\Projects\PracticeRecord\PracticeRecord','Random_string.py')
getFiles('D:\Documents\VisualStudio2015\Projects\PracticeRecord\PracticeRecord')
㈣ python 判斷是文件還是目錄
os.listdir("路徑")列出所有文件和目錄
os.path.isdir(「文件名」)判斷是目錄嗎,是返回True,不是返回False
os.path.isfile同上判斷文件
㈤ python如何用if判斷文件夾是否存在
python用if判斷文件夾是否存在的方法:
python的os模塊可以對文件夾進行操作。使用if語句「os.path.exists()」函數的返回值是否是True,如果是則輸出該文件夾存在
示例:判斷文件kk是否存在
代碼如下:
執行結果如下:
更多Python知識,請關註:Python自學網!!
㈥ python 判斷是文件夾還是文件
defdel_file_items(spath):
importos
paths=os.listdir(spath)
forpainpaths:
filepath=os.path.join(spath,pa)
ifos.path.isfile(filepath):
try:
os.remove(filepath)
exceptos.error:
print"remove%serror."%filePath
elifos.path.isdir(filepath):
try:
##在方法內可以引用自身
del_file_items(filepath)
exceptos.error:
print"remove%serror."%filePath
else:
os.rmdir(spath)
這段代碼給你做參考。
㈦ python 判斷list中的元素是否是需要的文件夾名稱
importos
path_base=r'C:pythonstudyfirstpartproqual_cemcases'
target_path=r'C:pythonstudyfirstpart'
alist=['1','2','3','4','5']
deffun(path):
files=[]
forroot,dirs,filesinos.walk(path):
forfninfiles:
files.append(os.path.join(root,fn))
returnfiles
forxinalist:
path=os.path.join(path_base,x)
ifos.path.isdir(path):
files=fun(path)
forfileinfiles:
filename=os.path.split(file)[1]
open(os.path.join(target_path,filename),"wb").write(open(file,"rb").read())
代碼未經測試,提供思路給你參考一下吧
㈧ python 判斷是文件還是文件夾
假設路徑為path
import os
isfile(path)#是否為文件
isdir(path)#是否為文件夾
exists(path)#是否存在此路徑
㈨ python 中怎樣判斷是不是文件
在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.
用法也很簡單
importos
filename='/tmp/test.txt'
printos.path.isfile(filename)
㈩ python判斷文件夾是否存在
$python
Python2.7.3(default,Jan22013,16:53:07)
[GCC4.7.2]onlinux2
Type"help","right","credits"or"license"formoreinformation.
>>>importos
>>>
>>>
>>>tobecheckdir=r'/home/tim/workspace'
>>>os.path.isdir(tobecheckdir)
True
>>>