python str.replace / str.endswith 以及 python语句(返回目录中所有JPG图像的文件名列表)

来源:互联网 发布:下载八爪鱼大数据 编辑:程序博客网 时间:2024/05/16 02:59


help(str.replace)


参数old:字符串S中的子字符串

参数new:用来替换old的新字符串

参数count:可选,如果指定count(整数),且该count数量小于old在S中的个数,则old会被new替换count次


string="asdffdsaasdffdsaasdf"old="as"new="12"string.replace(old, new)



################################################


str.endswith函数

help(str.endswith)


参数说明:

suffix:指定的后缀名,用于判断字符串S的后缀是否和其相同;suffix也可以使用一个字符串元组以进行多个比对

start:可选,用于字符串S在下标start开始

end:可选,停止比较S在下表end


判断S是否为jpg文件:

S="result.jpg"S.endswith('.jpg')str=('.jpg','.pgm','.bmp','.jpeg')S.endswith(str)



从start=7开始,此时S[start]='j'


从end=8结束,此时S[end]='p'



##################################################


返回目录中所有JPG图像的文件名列表

(python 语句)


import ospath=os.getcwd() #获取当前目录路径[os.path.join(path, f) for f in os.listdir(path) if f.endwith('.jpg')]




0 0
原创粉丝点击