本文目錄一覽:
- 1、python 如何實現兩個目錄下不同的文件,並輸出不同文件的路徑,將其寫入txt中
- 2、如何利用Python遍歷文件夾
- 3、Python中如何遍歷指定目錄下的所有文件?
- 4、如何用Python os.path.walk方法遍歷搜索文件內容的操作詳解
- 5、python 遞歸遍歷文件夾
python 如何實現兩個目錄下不同的文件,並輸出不同文件的路徑,將其寫入txt中
import os
def os_walker(folder):
“””遍歷foler裡面的文件”””
path = os.path.abspath(folder)
for root,dirs,files in os.walk(path):
if dirs:
continue
#print root,dirs,files
for f in files:
yield f, os.path.abspath(os.path.join(root,f))
def compare(f1, f2):
“”””對比出兩個文件夾裡面的文件””””
f1_list = {f:p for f,p in os_walker(f1)}
f2_list = {f:p for f,p in os_walker(f2)}
common = {_:f1_list[_] for _ in f1_list if _ in f2_list}
print “common: “, common
f1_specific = {_:f1_list[_] for _ in f1_list if _ not in f2_list}
print “f1_specific”, f1_specific
f2_specific = {_:f2_list[_] for _ in f2_list if _ not in f1_list}
print “f2_specific”, f2_specific
compare(“FOLDER1″,”FOLDER2”)
這個代碼有很多局限,比如沒考慮子目錄等,核心思想是用os.walk. 希望有幫助
如何利用Python遍歷文件夾
import os
for roots, dirs, files in os.walk(‘.’,topdown=True):
roots是所有的上層路徑
dirs是所有的目錄
files是所有的文件名
Python中如何遍歷指定目錄下的所有文件?
例如:在C:\TDDOWNLOAD目錄下有a.txt、b.txt兩個文件,另有\sub1子文件夾,C:\TDDOWNLOAD\sub1下又有c.txt、d.txt兩個文件。
1.
os.walk
os.walk()返回一個三元素的tuple:當前路徑、子文件夾名稱、文件列表。
import
os
def
fun(
path
):…
for
root,
dirs,
files
in
os.walk(
path
):…
for
fn
in
files:…
root,
fn…
fun(
r’C:\TDDOWNLOAD’
)C:\TDDOWNLOAD
a.txtC:\TDDOWNLOAD
b.txtC:\TDDOWNLOAD\sub1
c.txtC:\TDDOWNLOAD\sub1
d.txt
2.
glob.glob
glob.glob()只接受一個參數,這個參數既代有路徑,又代有匹配模式,返回值為一個列表。注意,glob.glob()無法直接穿透子文件夾,需要自己處理:
def
fun(
path
):…
for
fn
in
glob.glob(
path
+
os.sep
+
‘*’
):
#
‘*’代表匹配所有文件…
if
os.path.isdir(
fn
):
#
如果結果為文件夾…
fun(
fn
)
#
遞歸…
else:…
fn…
fun(
r’C:\TDDOWNLOAD’
)C:\TDDOWNLOAD\a.txtC:\TDDOWNLOAD\b.txtC:\TDDOWNLOAD\sub1\c.txtC:\TDDOWNLOAD\sub1\d.txt
‘*’為匹配模式,代表匹配所有文件,只有這樣才能將子文件夾查出來,以便遞歸深入,探查下一層的文件。
如何用Python os.path.walk方法遍歷搜索文件內容的操作詳解
本文是關於如何用Python os.path.walk方法遍歷搜索文件目錄內容的操作詳解的文章,python 代碼中用os.path.walk函數這個python模塊的方法來遍歷文件,python列出文件夾下的所有文件並找到自己想要的內容。
文中使用到了Python os模塊和Python sys模塊,這兩個模塊具體的使用方法請參考玩蛇網相關文章閱讀。
Python os.path.walk方法遍歷文件搜索內容方法代碼如下:
?
1234567891011121314151617181920212223242526272829303132333435363738394041
import os, sys#代碼中需要用到的方法模塊導入 listonly = False skipexts = [‘.gif’, ‘.exe’, ‘.pyc’, ‘.o’, ‘.a’,’.dll’,’.lib’,’.pdb’,’.mdb’] # ignore binary files def visitfile(fname, searchKey): global fcount, vcount try: if not listonly: if os.path.splitext(fname)[1] in skipexts: pass elif open(fname).read().find(searchKey) != -1: print’%s has %s’ % (fname, searchKey) fcount += 1 except: pass vcount += 1 # def visitor(args, directoryName,filesInDirectory): for fname in filesInDirectory: fpath = os.path.join(directoryName, fname) if not os.path.isdir(fpath): visitfile(fpath,args) def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 os.path.walk(startdir, visitor, searchkey) if __name__ == ‘__main__’: root=raw_input(“type root directory:”) key=raw_input(“type key:”) searcher(root,key) print ‘Found in %d files, visited %d’ % (fcount, vcount)
python 遞歸遍歷文件夾
沒有仔細看,但你的第一句就有錯
def distinguish_file(user_paht):
參數應為user_path
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/196939.html