【建站服务】高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设-域名申请
作者: 风兰 . 阅读量: 4 . 发表时间:2022-09-21 05:36:09
上往建站提供服务器空间服务商,百度快照排名,网站托管,百度推广运营,致力于设计外包服务与源代码定制开发,360推广,搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。
高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设

decide = input(' 文件 %s 中共有%s个【%s】 您确定要把所有的【%s】替换为【%s】吗? 【YES/NO】:'
% (file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
f_write = open(file_name, 'w')
f_write.writelines(content)
f_write.close()
f_read.close()
file_name = input('请输入文件名:')
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)
RyenToretto
RyenToretto
951***321@qq.com
5年前 (2017-10-12)
HantCoCo
zco***@163.com
37
在上面的例子中,write(),read() 方法默认是写入到当前 .py 同文件夹下面的,此外 w+ 的使用方法:不能直接 write() 后,在进行读取,这样试读不到数据的,因为数据对象到达的地方为文件最后,读取是向后读的,因此,会读到空白,应该先把文件对象移到文件首位
f = open("forwrite.txt", "w+",encoding='utf-8')
f.write("可以 ,你做的很好! 6666") # 此时文件对象在最后一行,如果读取,将读不到数据
s=f.tell() # 返回文件对象当前位置
f.seek(0,0) # 移动文件对象至第一个字符
str=f.read()
print(s,str,len(str))
HantCoCo
HantCoCo
zco***@163.com
5年前 (2018-02-05)
fatcheung
134***7025@qq.com
188
看上面分享的笔记,有个大佬打开文件然后没有关闭。。。
一般来说推荐以下方法:
#写
with open('test.txt', 'w', encoding='utf-8') as f:
f.write('test')
#读
with open('test.txt', 'r', encoding='utf-8') as f:
f.readlines()
执行完自动close,避免忘记关闭文件导致资源的占用。
fatcheung
fatcheung
134***7025@qq.com
5年前 (2018-02-23)
风逝
fen***ichenyao@qq.com
参考地址
52
由于文件读写时都有可能产生 IOError,一旦出错,后面的 f.close() 就不会调用。所以,为了保证无论是否出错都能正确的关闭文件,可以使用 try...finally 来实现:
try:
f = open('/path/to/file', 'r')
print(f.read())
finally:
if f:
f.close()
风逝
风逝
fen***ichenyao@qq.com
参考地址
3年前 (2019-07-07)
小花花
124***4671@qq.com
38
文本中替换字符串:
"""replace strings in text"""
import os
def Replace(file_name, rep_word, new_word):
with open(file_name) as f:
content = []
count = 0
for eachline in f:
if rep_word in eachline:
count += eachline.count(rep_word)
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('文件 {0} 中共有{1}个【{2}】 您确定要把所有的【{3}】替换为【{4}】吗? 【YES/NO】:'.format
(file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
with open(file_name, 'w') as f:
f.writelines(content)
print('Succeed!')
else:
print('Exit!')
if __name__ == '__main__':
while True:
高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设
上往建站提供搭建网站,域名注册,官网备案服务,网店详情页设计,企业网店,专业网络店铺管理运营全托管公司咨询电话,服务器空间,微信公众号托管,网页美工排版,致力于域名申请,竞价托管,软文推广,全网营销,提供标准级专业技术保障,了却后顾之忧,主营:虚拟主机,网站推广,百度竞价托管,网站建设,上网建站推广服务,网络公司有哪些等业务,专业团队服务,效果好。
服务热线:400-111-6878 手机微信同号:18118153152(各城市商务人员可上门服务)
关键词:网站建设,企业网站,网站制作,网页设计,高端网站建设,企业网站制作,网页制作,制作网站,网站设计,高端网页设计,高端网站设计,做网站,自适应网站



检索指定路径下后缀是 py 的所有文件:
#!/usr/bin/python3import osimport os.path#path = 'D:/UC/'ls = []def getAppointFile(path,ls): fileList = os.listdir(path) try: for tmp in fileList: pathTmp = os.path.join(path,tmp) if True==os.path.isdir(pathTmp): getAppointFile(pathTmp,ls) elif pathTmp[pathTmp.rfind('.')+1:].upper()=='PY': ls.append(pathTmp) except PermissionError: passdef main(): while True: path = input('请输入路径:').strip() if os.path.isdir(path) == True: break getAppointFile(path,ls) #print(len(ls)) print(ls) print(len(ls))main()领悟悟悟
437***659@qq.com
f123wk
f12***@xxx.com
获取文件后缀:
def getfile_fix(filename): return filename[filename.rfind('.')+1:]print(getfile_fix('runoob.txt'))f123wk
f12***@xxx.com
RyenToretto
951***321@qq.com
用户输入"xxx.txt"类文档文件名
用户输入被替换的"待替换字"
用户输入替换目标"新的字"
用户判断是否全部替换 yes/no
def file_replace(file_name, rep_word, new_word): f_read = open(file_name) content = [] count = 0 for eachline in f_read: if rep_word in eachline: count = count+eachline.count(rep_word) eachline = eachline.replace(rep_word, new_word) content.append(eachline) decide = input(' 文件 %s 中共有%s个【%s】 您确定要把所有的【%s】替换为【%s】吗? 【YES/NO】:' % (file_name, count, rep_word, rep_word, new_word)) if decide in ['YES', 'Yes', 'yes']: f_write = open(file_name, 'w') f_write.writelines(content) f_write.close() f_read.close()file_name = input('请输入文件名:')rep_word = input('请输入需