python读取文件指定位置(python读取文件夹下特定的文件)
Python 读取文件夹将里面的图片处理成想要的大小并保存在个指定位置
#?-*-?coding:?utf-8?-*-
import?cv2
import?os
import?numpy
import?cutHumanFace
def?saveCutFace(filePath,?pathSave?=?'cutFace',?normalizeWidth?=?300,?normalizeHeight?=?300):
????"""
????:param?filePath:?string,?文件夹路径
????"""
????
????if?not?os.path.exists(savePath):
????????os.makedirs(savePath);???????????????????#保存的文件夹
????????
????files?=?os.listdir(filePath);????????????????#列出目录下的所有文件
????
????normalizeWidth?=?100?????????????????????????#以100×100为大小
????normalizeHeight?=?100
????for?file?in?files:
????????normalizeFace?=?cv2.resize(cutFace,?(normalizeWidth,normalizeHeight),?interpolation=cv2.INTER_AREA);
????????cv2.imwrite(savePath,?normalizeFace);
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 print (os.path.dirname(__file__))print (os.path.abspath(__file__))print (os.path.abspath(os.path.dirname(__file__)))print (os.path.dirname(os.path.abspath(__file__)))
python怎么读取指定目录、指定文件、指定行的值呢? 麻烦回答的时候举个例子
对于文件,python通常是无法读取指定行的。不过是可以进行"曲线救国",但是这仅对文本文件生效,对于二进制文件,本身是没有行的概念的,讨论也没意义,下面是一种可能的解决方案。
path='c:\\documents'
filename='readme.txt'
fullfilename='%s\\%s'%(path,filename)
def?getContentByRowNumber(rownumber,filehandle):
????oldfilePos=filehandle.tell()
????i=0
????filehandle.seek(0)
????while?irownumber:
????????l=filehandle.readline()
????????if?not?l:
????????????return?None
????content=filehandle.readline()
????filehandle.seek(oldfilePos)
????return?content
f=open(filename,'rt')
print(getContentByRowNumber(2,f))