當前位置:首頁 » 文件管理 » python遍歷指定文件夾

python遍歷指定文件夾

發布時間: 2022-09-03 03:28:13

A. python 遞歸遍歷文件夾

沒有仔細看,但你的第一句就有錯
def distinguish_file(user_paht):
參數應為user_path

B. python獲取文件夾下所有文件名

使用os模塊可以獲取指定文件夾下所有文件名,有兩個方法os.walk()和os.listdir().
(1)os.walk可以用於遍歷指定文件下所有的子目錄、非目錄子文件。
(2)os.listdir()用於返回指定的文件夾下包含的文件或文件夾名字的列表,這個列表按字母順序排序。

C. python 遍歷文件夾PDF並轉換成圖片

這種圖片的也可以轉換成圖片的形式,一般文件夾要進行一個轉化才可以變成圖片。

D. 使用python遍歷指定文件,並將文件夾中所有的網址作為主頁下載這一頁中的內容

我覺得我沒明白你的問題,你是遍歷文件,哪來的文件夾中的網址?

你是想把文件中的所有網址都下載到本地?
你可以搜一下「python urllib.urlretrieve」,應該能找到你想要的代碼

E. python如何遍歷當前目錄下所有文件,並輸出後綴不為 py的文件名稱

#coding=utf-8
'''
Created on 2014-11-14

@author: Neo
'''
import os

def GetFileList(dir, fileList):
newDir = dir
if os.path.isfile(dir):
fileList.append(dir.decode('gbk'))
elif os.path.isdir(dir):
for s in os.listdir(dir):
#如果需要忽略某些文件夾,使用以下代碼
#if s == "xxx":
#continue
newDir=os.path.join(dir,s)
GetFileList(newDir, fileList)
return fileList

list = GetFileList('D:\\workspace\\PyDemo\\fas', [])
for e in list:
print e

F. python如何遍歷文件夾然後生成md5

importos,hashlib

defgetlistdir(path):
try:#如果path是一個文件的完整名稱,os.listdir會拋出錯誤
fl=os.listdir(path)
exceptExceptionase:
fl=[]
finally:
returnfl

defgetallfile(path):
allfile=[]
fl=getlistdir(path)
iflen(fl)!=0:
fl=list(map(lambdax:path+'\'+x,fl))
allfile=allfile+fl
forfinfl:
allfile=allfile+getallfile(f)
returnallfile

defmakemd5(stri):
md5=hashlib.md5()
md5.update(stri.encode('utf-8'))
returnmd5.hexdigest()

defmain():
myfilelist=getallfile('.')#獲取當前文件'.'中的所有文件和文件夾名list
myfilestr='|'.join(myfilelist)#文件list轉換為以'|'分隔的字元串
print(myfilestr)#顯示要進行md5摘要加密的字元
print("md5=",makemd5(myfilestr))#計算並顯示md5碼

main()

G. 求教python怎麼遍歷指定文件

監控目錄 -- 分兩個部分: 1. 掃描目錄文件, 保持當前狀態數據; 2. 狀態數據的比較



importos
importfnmatch


defgetfileinfo(filename):
(mode,ino,dev,nlink,
uid,gid,size,atime,mtime,ctime)=os.stat(filename)
returndict(
modifytime=mtime,
createtime=ctime,
size=size,
)


classDirectoryMonitor(object):

def__init__(self,path,fnexp="*.*"):
self.path=path
self.fnexp=fnexp
self.files={}
self.scan()

defscan(self):
currentfiles={}
forpath,dirs,filesinos.walk(self.path):
forfinfnmatch.filter(files,self.fnexp):
fullname=os.path.join(path,f)
currentfiles[fullname]=getfileinfo(fullname)
lastfiles=self.files
self.files=currentfiles
returnself.check(lastfiles,currentfiles)

@staticmethod
defcheck(lastfiles,currfiles):
monitor={}
newer={}
forfinset(currfiles)-set(lastfiles):
newer[f]=currfiles[f]
ifnewer:
monitor["newer"]=newer
deleted={}
forfinset(lastfiles)-set(currfiles):
deleted[f]=lastfiles[f]
ifdeleted:
monitor["deleted"]=deleted
changed={}
forfinset(lastfiles)&set(currfiles):
iflastfiles[f]!=currfiles[f]:
changed[f]=currfiles[f]
ifchanged:
monitor["changed"]=changed
returnmonitor


deftester():
importtime
dm=DirectoryMonitor(r"/home/tim/data","*.txt")
time.sleep(20)
m=dm.scan()
ifm:
printm


if__name__=="__main__":
tester()

H. python如何實現for循環操作文件

python用for循環遍歷文件操作,代碼如下:

#!ursinenvpython
#encoding:utf-8#設置編碼方式
importos
importre
classloop_file:
def__init__(self,root_dir,short_exclude=[],long_exclude=[],file_extend=[]):
self.root_dir=root_dir
self.short_exclude=short_exclude
self.long_exclude=long_exclude
self.file_extend=file_extend
def__del__(self):
pass
defstart(self,func):
self.func=func
returnself.loop_file(self.root_dir)
defloop_file(self,root_dir):
t_sum=[]
sub_gen=os.listdir(root_dir)
forsubinsub_gen:
is_exclude=False
forextendsinself.short_exclude:##在不檢查文件、目錄范圍中
ifextendsinsub:##包含特定內容
is_exclude=True
break
ifre.search(extends,sub):##匹配指定正則
is_exclude=True
break
ifis_exclude:
continue
abs_path=os.path.join(root_dir,sub)
is_exclude=False
forexcludeinself.long_exclude:
ifexclude==abs_path[-len(exclude):]:
is_exclude=True
break
ifis_exclude:
continue
ifos.path.isdir(abs_path):
t_sum.extend(self.loop_file(abs_path))
elifos.path.isfile(abs_path):
ifnot"."+abs_path.rsplit(".",1)[1]inself.file_extend:##不在後綴名檢查范圍中
continue
t_sum.append(self.func(abs_path))
returnt_sum
if'__main__'==__name__:
root_dir=r'D:harness ewshoppingcart estcasepromosingle_promo'
short_exclude=['.svn','.*_new.rb']###不包含檢查的短目錄、文件
long_exclude=[]###不包含檢查的長目錄、文件
file_extend=['.rb']###包含檢查的文件類型
lf=loop_file(root_dir,short_exclude,long_exclude,file_extend)
forfinlf.start(lambdaf:f):
printf

I. 求通過python實現,在指定目錄下遍歷所有文件,將以.txt為後綴的文件移動到另一指定目錄中

target_dir = 'home/' #假定要拷貝到home目錄
x = [ item for item in os.walk('.') ] #os.walk遞歸地遍歷所有子文件夾
#返回的是一個list,list中每一個元素由3個部分:(path, dirs, files)
for path, dirs, files in x:
for file in files:
if file.endswith('.txt'): #找到以txt結尾的,之
shutil.( path+os.sep+file , target_dir )

熱點內容
啟動腳本執行器失敗怎麼解決 發布:2024-10-12 14:12:32 瀏覽:583
安卓系統如何設置息屏時間 發布:2024-10-12 14:11:58 瀏覽:829
配置資源需要什麼條件 發布:2024-10-12 14:07:37 瀏覽:259
java資料庫連接的url 發布:2024-10-12 14:03:25 瀏覽:783
c資料庫類型 發布:2024-10-12 13:51:17 瀏覽:922
qq上傳視頻大小 發布:2024-10-12 13:38:37 瀏覽:458
編程中的非0 發布:2024-10-12 13:37:09 瀏覽:241
如何查詢寬頻運營商賬號密碼 發布:2024-10-12 13:34:50 瀏覽:665
兩個伺服器分別是什麼意思 發布:2024-10-12 13:27:37 瀏覽:859
我的世界寶可夢伺服器電腦和手機可以聯機 發布:2024-10-12 13:11:59 瀏覽:782