收藏此站 联系我们 网站
当前位置:首页» 行业新闻 » 【建站服务】高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设-域名申请

【建站服务】高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设-域名申请

作者: 风兰 . 阅读量: 4 . 发表时间:2022-09-21 05:36:09

网站建设

上往建站提供服务器空间服务商百度快照排名网站托管百度推广运营,致力于设计外包服务与源代码定制开发360推广搜狗推广,增加网站的能见度及访问量提升网络营销的效果,主营:网站公司,百度推广公司电话,官网搭建服务,网站服务企业排名,服务器空间,英文域名等业务,专业团队服务,效果好。


高端!网站定制/设计/优化/二次开发/制作/搭建-广安高端网站建设

网站建设.png

  1. 检索指定路径下后缀是 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

    5年前 (2017-08-03)
  2.    f123wk

      f12***@xxx.com

    48

    获取文件后缀:

    def getfile_fix(filename):
         return filename[filename.rfind('.')+1:]print(getfile_fix('runoob.txt'))
    f123wk

       f123wk

      f12***@xxx.com

    5年前 (2017-10-09)
  3.    RyenToretto

      951***321@qq.com

    43

    用户输入"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('请输入需



    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(各城市商务人员可上门服务)


关键词:网站建设,企业网站,网站制作,网页设计,高端网站建设,企业网站制作,网页制作,制作网站,网站设计,高端网页设计,高端网站设计,做网站,自适应网站

全国服务热线
18114747181
二维码
手机端二维码
上往建站
地址:全国各地都有驻点商务 |  网站建设上往建站
在线咨询QQ:1120768800
 
QQ在线咨询
售前咨询热线
18114747181
营销顾问
营销顾问
售后服务热线
400-000-1116
售后服务
售后服务