當前位置:首頁 » 編程語言 » python搜索文件

python搜索文件

發布時間: 2022-04-02 10:41:24

❶ 如何用python寫一個文件搜索器

好好看一下OS模塊,比如用os.walk()來遍歷文件夾里的所有文件,模糊判斷是否存在與需要搜索的文件同名的文件

❷ 怎麼用python搜索文本並篩選出來

txtfile=open(r'test.txt',"r")
newtxtfile=open(r'new.txt',"w")
linelist=[]
forlineintxtfile:
linelist.append(line)
iflen(linelist)==4:
ifnotlinelist[1].startswith(r'aaa'):
newtxtfile.writelines(linelist)
linelist=[]
iflen(linelist)>1:
ifnotlinelist[1].startswith(r'aaa'):
newtxtfile.writelines(linelist)
eliflen(linelist)==1:
newtxtfile.writelines(linelist)
txtfile.close()
newtxtfile.close()

讀取文件test.txt,將每四行中第二行以aaa開始的去除,寫入新文件new.txt中

❸ 如何用Python實現查找"/"目錄下的文件夾或文件,感謝

給你各相對來說容易理解的哈
import os
name=raw_input('filename:') #在這里輸入你的查找值
a=os.listdir('/') #把所有/目錄下的文件,目錄存放入a
if name in a: #如果查找值在/目錄下,進行進一步判斷
if os.path.isdir(name): #判斷是否為目錄

print 'dir'

elif os.path.isfile(name) and os.pathislink(name): #符號連接即是文件又是link所以雙重判斷

print 'link'

elif os.path.isfile(name): #判斷是否文件

print 'file'

else: #linux上文件類型多,不符合上面三種列印0ther

print 'other'
else: #不存在列印『not exist』
print 'not exist'

❹ 如何用Python os.path.walk方法遍歷搜索文件內容的操作詳解

本文是關於如何用Python os.path.walk方法遍歷搜索文件目錄內容的操作詳解的文章,python 代碼中用os.path.walk函數這個python模塊的方法來遍歷文件,python列出文件夾下的所有文件並找到自己想要的內容。
文中使用到了Python os模塊和Python sys模塊,這兩個模塊具體的使用方法請參考玩蛇網相關文章閱讀。
Python os.path.walk方法遍歷文件搜索內容方法代碼如下:
?

041
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 #www.iplaypy.com 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,如何查找一個文件夾里的最新的文件

每次掃描完生成一個文件
以後每次掃描都拿這個文件跟所有文件的修改時間做比較,比那個文件新的就是更新了

❻ python,如何查找一個文件夾里的最新產生的文件並且得到最新文件生成的時間

# -*- coding:UTF-8 -*-
import os,os.path,datetime
base_dir="c:\Windows\"
l=os.listdir(base_dir)
l.sort(key=lambda fn: os.path.getmtime(base_dir+fn) if not os.path.isdir(base_dir+fn) else 0)
d=datetime.datetime.fromtimestamp(os.path.getmtime(base_dir+l[-1]))
print('最後改動的文件是'+l[-1]+",時間:"+d.strftime("%Y年%m月%d日 %H時%M分%S秒"))


>>>
最後改動的文件是WindowsUpdate.log,生成時間:2013年04月10日 12時18分09秒


這個算較簡的方法。注意第5、6行在同一行上,網路自動斷了。

python3.2代碼

❼ 用python搜索文件夾內所有文件,並且根據名字打開其他文檔

importglob,os,re

path_a='e:\A'
path_b='e:\B'

a_files=glob.glob('%s\*'%path_a)
b_files=glob.glob('%s\*'%path_b)

forfina_files:
file_name=os.path.basename(f)
file_name_in_folder_b=re.subn(ur'd{8}_d{2}_d{2}_d{2}_','',file_name)
full_path='%s\%s'%(path_b,file_name_in_folder_b)
iffull_pathinb_files:
file_in_b=open(full_path,'r')

❽ 用python實現一個本地文件搜索功能。

import re,os
import sys
def filelist(path,r,f):
"""
function to find the directions and files in the given direction
according to your parameters,fileonly or not,recursively find or not.
"""
file_list = []
os.chdir(path)
filename = os.listdir(path)
if len(filename) == 0:
os.chdir(os.pardir)
return filename
if r == 1: ##
if f == 0: # r = 1, recursively find directions and files. r = 0 otherwise.
for name in filename: # f = 1, find files only, f = 0,otherwise.
if os.path.isdir(name): ##
file_list.append(name)
name = os.path.abspath(name)
subfile_list = filelist(name,r,f)
for n in range(len(subfile_list)):
subfile_list[n] = '\t'+subfile_list[n]
file_list += subfile_list
else:
file_list.append(name)
os.chdir(os.pardir)
return file_list
elif f == 1:
for name in filename:
if os.path.isdir(name):
name = os.path.abspath(name)
subfile_list = filelist(name,r,f)
for n in range(len(subfile_list)):
subfile_list[n] = '\t'+subfile_list[n]
file_list += subfile_list
else:
file_list.append(name)
os.chdir(os.pardir)
return file_list
else:
print 'Error1'
elif r == 0:
if f == 0:
os.chdir(os.pardir)
return filename
elif f == 1:
for name in filename:
if os.path.isfile(name):
file_list.append(name)
os.chdir(os.pardir)
return file_list
else:
print 'Error2'
else:
print 'Error3'

'''
f = 0:list all the files and folders
f = 1:list files only
r = 1:list files or folders recursively,all the files and folders in the current direction and subdirection
r = 0:only list files or folders in the current direction,not recursively

as for RE to match certern file or dirction,you can write yourself, it is easier than the function above.Just use RE to match the result,if match,print;else,pass
"""

熱點內容
用什麼緩存視頻 發布:2025-07-13 14:52:27 瀏覽:360
GTA5mods文件夾 發布:2025-07-13 14:52:27 瀏覽:52
如何在本地搭建svn伺服器地址 發布:2025-07-13 14:52:26 瀏覽:963
手動編程教程 發布:2025-07-13 14:52:17 瀏覽:377
編程要求學歷 發布:2025-07-13 14:50:40 瀏覽:186
投資黃金源碼 發布:2025-07-13 14:48:49 瀏覽:881
androidservice的生命周期 發布:2025-07-13 14:44:24 瀏覽:669
c語言另存 發布:2025-07-13 14:42:29 瀏覽:279
腳本的寫作 發布:2025-07-13 14:42:27 瀏覽:80
python多行匹配 發布:2025-07-13 14:42:14 瀏覽:47