導航:首頁 > 編程語言 > python讀取文件夾內容

python讀取文件夾內容

發布時間:2022-07-31 05:36:18

python怎麼讀取某個文件夾下的全部文件

import os
path=raw_input('enter the path:')
os.chdir(path)
files=os.listdir(path)

㈡ python怎麼讀取文件夾內容

#encoding:utf-8
import os
#設置文件夾所在路徑,我這里設置哦當前路徑
path = './'
#列出路徑下所有的一級目錄+文件
files = os.listdir(path)
print files
#利用遞歸,列出目錄下包括子目錄所有的文件及文件夾(但是沒有分級,如果需要分級,自己寫吧)
files1 = []
def listfiles(path):
for i in os.listdir(path):
if os.path.isdir(path+i):
files1.append(i)
listfiles(path+i)
else:
files1.append(i)
listfiles(path)
print files1

㈢ python如何讀取文件的內容

# _*_ coding: utf-8 _*_

import pandas as pd

# 獲取文件的內容

def get_contends(path):

with open(path) as file_object:

contends = file_object.read()

return contends

# 將一行內容變成數組

def get_contends_arr(contends):

contends_arr_new = []

contends_arr = str(contends).split(']')

for i in range(len(contends_arr)):

if (contends_arr[i].__contains__('[')):

index = contends_arr[i].rfind('[')

temp_str = contends_arr[i][index + 1:]

if temp_str.__contains__('"'):

contends_arr_new.append(temp_str.replace('"', ''))

# print(index)

# print(contends_arr[i])

return contends_arr_new

if __name__ == '__main__':

path = 'event.txt'

contends = get_contends(path)

contends_arr = get_contends_arr(contends)

contents = []

for content in contends_arr:

contents.append(content.split(','))

df = pd.DataFrame(contents, columns=['shelf_code', 'robotid', 'event', 'time'])

(3)python讀取文件夾內容擴展閱讀:

python控制語句

1、if語句,當條件成立時運行語句塊。經常與else, elif(相當於else if) 配合使用。

2、for語句,遍歷列表、字元串、字典、集合等迭代器,依次處理迭代器中的每個元素。

3、while語句,當條件為真時,循環運行語句塊。

4、try語句,與except,finally配合使用處理在程序運行中出現的異常情況。

5、class語句,用於定義類型。

6、def語句,用於定義函數和類型的方法。

㈣ python中怎麼讀取文件內容

用open命令打開你要讀取的文件,返回一個文件對象
然後在這個對象上執行read,readlines,readline等命令讀取文件
或使用for循環自動按行讀取文件

㈤ 請問大神怎樣用python批量讀取文件夾下的文件

importos
defsearch(s,path=os.path.abspath('.')):
forzinos.listdir(path):
ifos.path.isdir(path+os.path.sep+z):
print('Currnet:',path)
path2=os.path.join(path,z)
print('future:',path2)
search(s,path2)
elifos.path.isfile(path+os.path.sep+z):
ifsinz:
print(os.path.join(path,z))
withopen(path+os.path.sep+z,'r')asfr:
withopen('save.txt','a')asfw:
fw.write(path+' '+fr.read())
search('csv','.')

㈥ python 隨機讀取文件夾內一個文本文件

來個批量讀取文件的例子:

defread_test_data():
forparent,dirnames,filenamesinos.walk(pbx_test_path):
forfilenameinfilenames:
iffilename[:5]=="TEST_":
file_path=pbx_test_path+"\%s"%filename
#file_bak_path=pbx_test_path_bak+"\%s"%filename
ifos.path.isfile(file_path):
#printfile_path
read_by_file(file_path)
#shutil.file(file_path,file_bak_path)
try:
os.remove(file_path)
exceptWindowsError:
pass

defread_by_file(file_path):
file_object=open(file_path)
count=0
thefile=open(file_path,'rb')
whileTrue:
buffer=str(thefile.readline())
ifnotbuffer:
break
all_test_list=buffer.split(";")
all_test_list=all_test_list[:len(all_test_list)-1]#去除回車行
zian_str=""
value_str=""
fori,testinenumerate(all_test_list):
test_attr_list=test.split("=")
zian_str+=""+test_attr_list[0]+","
value_str+="'"+test_attr_list[1]+"',"
zian_str="("+zian_str[:len(zian_str)-1]+")"
value_str="("+value_str[:len(value_str)-1]+")"
sql_str="insertintopbx_test"+zian_str+"values"+value_str+""
#mssql.insert(sql_str)
session.execute(sql_str)
count+=buffer.count(' ')
commit()
thefile.close()

㈦ Python如何從文件讀取數據

1.1 讀取整個文件

要讀取文件,需要一個包含幾行文本的文件(文件PI_DESC.txt與file_reader.py在同一目錄下)

PI_DESC.txt

3.1415926535
8979323846
2643383279
5028841971

file_reader.py

with open("PI_DESC.txt") as file_object:
contents = file_object.read()
print(contents)

我們可以看出,讀取文件時,並沒有使用colse()方法,那麼未妥善的關閉文件,會不會導致文件收到損壞呢?在這里是不會的,因為我們在open()方法前邊引入了關鍵字with,該關鍵字的作用是:在不需要訪問文件後將其關閉

1.2文件路徑

程序在讀取文本文件的時候,如果不給定路徑,那麼它會先在當前目錄下進行檢索,有時候我們需要讀取其他文件夾中的路徑,例如:

㈧ 如何用Python打開指定的某個文件夾比如我要用Python打開D盤里的一個普通文件,該怎麼做

操作系統一般不允許程序直接操作磁碟文件的,文件在磁碟上的讀寫都是由操作系統提供的,文件操作包括創建、刪除、修改許可權、讀取、寫入等。要想實現對文件的操作就需要請求操作系統打開一個文件對象,然後通過操作系統提供的介面來實現對文件讀寫的相關操作。讀寫文件是最常見的 I/O 操作,Python內置相關API可以幫助我們快速的實現文件讀寫操作。

文件對象
Python內置的 open() 函數可以用於打開指定文件 ,該函數會返回一個文件對象(該對象包含了當前文件擁有的屬性信息,相當於文件句柄)。參數file為要創建或打開文件的文件名稱,參數mode用於指定文件的打開模式(可選),參數buffering用於指定對文件做讀寫操作時是否使用緩沖區。在打開文件之後就可調用文件對象的屬性和方法,完成對文件的讀/寫操作之後最後需要關閉該文件,通過文件對象的close() 函數來實現即可。

打開模式
文件打開是可以指定其打開的模式,該參數通過mode來指定【默認模式為只讀(r)】。選擇不同的打開模式決定了後續可以對文件進行操作,如果使用 r 模式打開的文件則只能讀取文件而無法修改文件內容。open()提供了多種打開模式,下面列出了對應的打開模式和說明:

㈨ python 怎麼讀取文件夾中的數據

由於項目需求收集並使用過一些爬蟲相關庫,做過一些對比分析。以下是我接觸過的一些庫: Beautiful Soup。名氣大,整合了一些常用爬蟲需求。缺點:不能載入JS。 Scrapy。

㈩ python 怎麼讀取當前目錄下指定文件

讀文本文件
input = open('data', 'r')
#第二個參數默認為r
input = open('data')

讀二進制文件
input = open('data', 'rb')

讀取所有內容
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

讀固定位元組
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )

讀每行
list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,還可以直接遍歷文件對象獲取每行:
for line in file_object:
process line

閱讀全文

與python讀取文件夾內容相關的資料

熱點內容
php開發客戶端 瀏覽:998
theisle測試服怎麼搜伺服器 瀏覽:447
廣播PDF 瀏覽:218
單片機編程300例匯編百度 瀏覽:35
騰訊雲連接不上伺服器 瀏覽:223
不能用來表示演算法的是 瀏覽:861
6軸機器人演算法 瀏覽:890
手機主題照片在哪個文件夾 瀏覽:294
安卓手機後期用什麼軟體調色 瀏覽:628
cad修改快捷鍵的命令 瀏覽:242
好錢包app怎麼登錄不了 瀏覽:859
樹莓派都用python不用c 瀏覽:757
access文件夾樹的構造 瀏覽:662
安卓多指操作怎麼設置 瀏覽:658
linux樹形目錄 瀏覽:727
平方根的簡單演算法 瀏覽:898
千牛訂單頁面信息加密取消 瀏覽:558
單片機自製紅外遙控燈 瀏覽:719
伺服器最小配置怎麼弄 瀏覽:853
ibm伺服器硬體如何升級 瀏覽:923