包含python怎么读取文件夹里部分的文件的词条

http://www.itjxue.com  2023-04-03 07:06  来源:未知  点击次数: 

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读取txt文件里指定行的内容,并导入excel?

全文使用的是xlswriter模块写的,也有人使用?xlrd与?xlutils模块实现,不过还未进行验证

import xlsxwriter

workbook = xlsxwriter.Workbook("D:\\Program Files\\subpy\\sql2.xlsx")#在指定目录下创建一个excle

worksheet = workbook.add_worksheet("students")#新建一个sheet

title_index = ["A","B","C","D"]#sheet中的区域

li = [] #定义一个空列表

blod = workbook.add_format({"bold":True})#定义exlce中写入的字体with open("D:\\Program Files\\subpy\\tets.txt",'r') as f1:#打开txt文档

lines = f1.readlines()#读取所有行内容

n = -1#定义一个变量

for x in lines:#逐行读取

n=n+1

li.append(x[:-1])#去掉回车符

y= x.split#以空格分字符

for i in range(len(title_index)):#读取excle区域下标

# for i,j in enumerate(title_index):

content = y[i]#单个字符读取

worksheet.write(n,i,content,blod)#分行分列写入workbook.

close#关闭excle

txt文件可以用行号,用readlines读取出来的数据是一个列表,你可以使用:

f = open('', 'r')

line = f.readlines()

line_need = line[行号-1]

这样来取指定行

请问大神怎样用python批量读取文件夹下的文件?

import?os

def?search(s,?path=os.path.abspath('.')):

????for?z?in?os.listdir(path):

????????if?os.path.isdir(path?+?os.path.sep?+?z):

????????????print('Currnet:',?path)

????????????path2?=?os.path.join(path,?z)

????????????print('future:',?path2)

????????????search(s,?path2)

????????elif?os.path.isfile(path?+?os.path.sep?+?z):

????????????if?s?in?z:

????????????????print(os.path.join(path,?z))

????????????????with?open(path?+?os.path.sep?+?z,?'r')?as?fr:

????????????????????with?open('save.txt',?'a')?as?fw:

????????????????????????fw.write(path?+?'\t'?+?fr.read())

search('csv',?'.')

批量阅读文件夹里的文件 python

#!/usr/bin/env?python3.6

from?pathlib?import?Path

def?read_all_txt(dirname):

????ss?=?[]

????for?p?in?Path(dirname).rglob('*.txt'):

????????ss.append(p.read_text())

????return?ss

python 随机读取文件夹内一个文本文件

来个批量读取文件的例子:

def?read_test_data():

????for?parent,dirnames,filenames?in?os.walk(pbx_test_path):

????????for?filename?in?filenames:

????????????if?filename[:5]?==?"TEST_":

????????????????file_path?=?pbx_test_path+"\%s"?%?filename

????????????????#file_bak_path?=?pbx_test_path_bak+"\%s"?%?filename

????????????????if?os.path.isfile(file_path):

????????????????????#print?file_path

????????????????????read_by_file(file_path)

????????????????????#shutil.copyfile(file_path,file_bak_path)

????????????????????try:

????????????????????????os.remove(file_path)

????????????????????except??WindowsError:

????????????????????????pass

?????????????????

def?read_by_file(file_path):

????file_object?=?open(file_path)

????count?=?0

????thefile?=?open(file_path,?'rb')

????while?True:

????????buffer?=?str(thefile.readline())

????????if?not?buffer:

????????????break

????????all_test_list?=?buffer.split(";")

????????all_test_list?=?all_test_list[:len(all_test_list)-1]#去除回车行

????????ziduan_str?=?""

????????value_str?=?""

????????for?i,test?in?enumerate(all_test_list):

????????????test_attr_list?=?test.split("=")

????????????ziduan_str?+=?""+test_attr_list[0]+","

????????????value_str?+=?"'"+test_attr_list[1]+"',"

????????ziduan_str?=?"("+ziduan_str[:len(ziduan_str)-1]+")"

????????value_str?=?"("+value_str[:len(value_str)-1]+")"

????????sql_str?=?"insert?into?pbx_test"+ziduan_str+"?values"+value_str+""

????????#mssql.insert(sql_str)

????????session.execute(sql_str)

????????count?+=?buffer.count('\n')

????commit()

????thefile.close()

用Python读取指定txt文件中的部分内容

程序代码如下:

import?os

mxdPath=r"F:\res\高程点.txt"

fpa=open(mxdPath)

fp-1=open("1.txt","w")

fp-2=open("2.txt","w")

for?linea?in?fpa.readlines():

????linea=linea.replace("\n","").split("\t")

????fp-1.writeline(linea[0])

????fp-2.writeline(linea[1])

fpa.close()

fp-1.close()

fp-2.close()

首先打开数据存放的文件进行,读操作;

然后打开两个写文件,如果不存在,可以直接创建;

按行读取,然后分割,分别写入不同的文件。

(责任编辑:IT教学网)

更多

推荐SQL Server文章