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

python文件搜索

發布時間: 2023-03-06 02:08:28

python從文件中查找數據並輸出

#注意,這里的代碼用單空格縮進
importre

#寫上你的文件夾路徑
yourdir=""

keywordA="keywordA"

keywordB="keywordA(d+)"

files=[os.path.join(yourdir,f)forfinos.listdir(yourdir)]

withopen("out.txt","w")asfo:
forfinfiles:
fname=os.path.basename(f)
withopen(f,"r")asfi:
forlineinfi:
ifline.strip():
searchA=re.search(keywordA,line.strip())
ifsearchA:
searchB=re.search(keywordB,line.strip())
ifsearchB:
print(fname,serachB.groups()[0],sep=" ",file=fo)

㈡ 如何設置python模塊的默認搜索路徑

Python 的模塊搜索路徑 PYTHONPATH 類似 Java 的 CLASSPATH
由以下路徑組合而成,通過更改以下 4 者其中之一都可以改變 Python 模塊的默認搜索路徑:
1. 當前工作目錄
2. 操作系統的 PYTHONPATH 環境變數
3. Python 安裝目錄 lib/site-packages 中或 PYTHONPATH 中的 .pth 文件定義的路徑
如:find ~/my_lib -type d -print > ~/python2.6/site-packages/my_lib.pth
4. 運行時對 sys.path 的改變,如:

向後增加:
sys.path.append(path)
path 不要以 / 或 \ 結尾。
向前增加:
sys.path.insert(0, path)

㈢ 用Python查找某文件夾的文件名稱,並對文件名進行更改的庫名稱是什麼

要在文件夾中搜索文件名並在 Python 中重命名文件,可以使用模塊和模塊。該模塊提供基於通配符模式匹配文件路徑的函數,而該模塊提供用於處理文件和文件夾(如重命名文件)的函數。globosglobos

下面是如何使用 and 模塊在文件夾中搜索文件名並重命名文件的示例:globos

此代碼使用該函數搜索指定文件夾中的文件,使用通配符模式匹配所有文件。然後,它循環訪問文件名,並使用該函數將文件名拆分為其基本名稱和擴展名。然後,它會向基名稱添加新後綴,並使用該函數重命名文件。glob.glob()*os.path.splitext()os.rename()

總體而言,和模塊提供了一種強大而靈活的方式來搜索文件夾中的文件名並在 Python 中重命名文件。您可以使用這些模塊來實現各種文件管理任務,例如基於不同模式搜索文件、批量重命名文件以及在文件夾之間移動或復制文件。

回答不易望請採納

㈣ 用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')

㈤ R、python的文件夾操作

Python OS模塊

1.重命名:os.rename(old, new)
2.刪除:os.remove(file)
3.列出目錄下的 文件 :os.listdir(path)
4.獲取當前工作目錄:os.getcwd()
5.改變工作目錄:os.chdir(newdir)
6.創建多級目錄:os.makedirs(r"c:/python /test")
7.創建單個目錄:os.mkdir("test")
8.刪除多個目錄:os.removedirs(r"c:/python") #刪除所給路徑最後一個目錄下所有空目錄。
9.刪除單個目錄:os.rmdir("test")
10.獲取文件屬性:os.stat(file)
11.修改文件許可權與時間戳:os.chmod(file)
12.執行操作系統 命令:os.system("dir")
13.啟動新進程:os.exec(), os.execvp()
14.在後台執行程序:osspawnv()
15.終止當前進程:os.exit(), os._exit()
16.分離文件名:os.path.split(r"c:/python/ hello.py ") –> ("c://python", " hello.py ")
17.分離擴展名:os.path.splitext(r"c:/python/ hello.py ") –> ("c://python//hello", ".py")
18.獲取路徑名:os.path.dirname(r"c:/python/ hello.py ") –> "c://python"
19.獲取文件名:os.path.basename(r"r:/python/hello.py") –> "hello.py"
20.判斷文件是否存在:os.path.exists(r"c:/python/hello.py") –> True
21.判斷是否是絕對路徑:os.path.isabs(r"./python/") –> False
22.判斷是否是目錄:os.path.isdir(r"c:/python") –> True
23.判斷是否是文件:os.path.isfile(r"c:/python/hello.py") –> True
24.判斷是否是鏈接文件:os.path.islink(r"c:/python/hello.py") –> False
25.獲取文件大小:os.path.getsize(filename)
26.*******:os.ismount("c://") –> True
27.搜索目錄下的所有文件:os.path.walk()
[2.shutil]
1.復制單個文件:shultil.(oldfile, newfle)
2.復制整個目錄樹:shultil.tree(r"./setup", r"./backup")
3.刪除整個目錄樹:shultil.rmtree(r"./backup")
[3.tempfile]
1.創建一個唯一的臨時文件:tempfile.mktemp() –> filename
2.打開臨時文件:tempfile.TemporaryFile()
[4.StringIO] #cStringIO是StringIO模塊的快速實現模塊
1.創建內存 文件並寫入初始數據 :f = StringIO.StringIO("Hello world!")
2.讀入內存文件數據:print f.read() #或print f.getvalue() –> Hello world!
3.想內存文件寫入數據:f.write("Good day!")
4.關閉內存文件:f.close()

rm(list=ls())
path = 'J:/lab/EX29 --在R語言中進行文件(夾)操作'
setwd(path)
cat("file A\n", file="A") #創建一個文件A,文件內容是'file A','\n'表示換行,這是一個很好的習慣
cat("file B\n", file="B") #創建一個文件B
file.append("A", "B") #將文件B的內容附到A內容的後面,注意沒有空行
file.create("A") #創建一個文件A, 注意會覆蓋原來的文件
file.append("A", rep("B", 10)) #將文件B的內容復制10便,並先後附到文件A內容後
file.show("A") #新開工作窗口顯示文件A的內容
file.("A", "C") #復制文件A保存為C文件,同一個文件夾
dir.create("tmp") #創建名為tmp的文件夾
file.(c("A", "B"), "tmp") #將文件夾拷貝到tmp文件夾中
list.files("tmp") #查看文件夾tmp中的文件名
unlink("tmp", recursive=F) #如果文件夾tmp為空,刪除文件夾tmp
unlink("tmp", recursive=TRUE) #刪除文件夾tmp,如果其中有文件一並刪除
file.remove("A", "B", "C") #移除三個文件

㈥ 用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
"""

㈦ 怎麼用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中

熱點內容
手機f加密 發布:2024-05-19 03:28:47 瀏覽:225
用什麼密碼寫日記 發布:2024-05-19 03:27:57 瀏覽:303
資料庫access2003 發布:2024-05-19 02:49:39 瀏覽:620
碧藍航線pc掛機腳本 發布:2024-05-19 02:30:03 瀏覽:589
腳本fir 發布:2024-05-19 02:28:57 瀏覽:261
阿里雲獨享伺服器 發布:2024-05-19 02:23:54 瀏覽:253
織夢源碼ga 發布:2024-05-19 02:23:20 瀏覽:571
java文件名後綴 發布:2024-05-19 02:14:39 瀏覽:956
快手點榜腳本 發布:2024-05-19 02:08:44 瀏覽:163
pythonforinkeys 發布:2024-05-19 01:55:44 瀏覽:793