头歌实践平台(Educoder):python 教学案例十二 文件处理

2024-06-08 1926阅读

第1关 读取唐诗文件,并根据诗人建立多个文件夹

头歌实践平台(Educoder):python 教学案例十二 文件处理
(图片来源网络,侵删)
import os
import shutil
if os.path.exists("wjcl/src/step4/tssr"):
    shutil.rmtree("wjcl/src/step4/tssr")
os.mkdir("wjcl/src/step4/tssr")
f1=open("wjcl/src/step1/唐诗.txt",'r')
#代码开始
for line in f1:
    if line[:3].isdigit():
        a=line[3:line.find(":")]
        b="wjcl/src/step4/tssr//"+a
        if not os.path.isdir(b):
            os.mkdir(b)
f1.close()
#代码结束

第2关 读取唐诗文件,根据诗人建立多个文件

import shutil
import os
if os.path.exists("wjcl/src/step3/ts"):
    shutil.rmtree("wjcl/src/step3/ts")
os.mkdir("wjcl/src/step3/ts")
f1=open("wjcl/src/step1/唐诗.txt",'r')
#代码开始
for line in f1:
    if line[:3].isdigit():
        srxm=line[3:line.find(":")]
        wj="wjcl/src/step3/ts/"+srxm+".txt"
        f2=open(wj,"a")
    if len(line.strip())>0:
        f2.write(line)
#代码结束
f1.close()
f2.close()

第3关 读取唐诗文件,为每首诗建立文本文件

import os
import shutil
if  os.path.exists("wjcl/src/step5/ts"):
    shutil.rmtree("wjcl/src/step5/ts")
os.mkdir("wjcl/src/step5/ts")
f1=open("wjcl/src/step1/唐诗.txt",'r')
#代码开始
for line in f1:
    if line[:3].isdigit():
        a=line[3:line.find(":")]
        c=line[line.find(":")+1:].strip() 
        d="wjcl/src/step5/ts/"+a 
        if not os.path.exists(d):
            os.mkdir(d)
        b="wjcl/src/step5/ts/"+a+"/"+c+".txt" 
        f2=open(b,"a") 
        f2.write(c+"\n") 
        f2.write(a+"\n")
    elif len(line.strip())>0:
        f2.write(line)
f1.close() 
f2.close()
#代码结束

第4关 文件的复制文件的复制

import os
import shutil
lj="wjcl/src/素材"
lj1="wjcl/素材"
if os.path.exists(lj):
    shutil.rmtree(lj)  
shutil.copytree(lj1,lj)
#代码开始
a=lj+"/风景图片"
if not os.path.exists(a):
    os.mkdir(a) 
b=os.listdir(lj1) 
for x in b:
    if os.path.isdir(lj1+"/"+x) and x!="风景图片"and x!=".gitkeep":
        c=0
        y=os.listdir(lj1+"/"+x) 
        for i in y:
            ywj=lj1+"/"+x+"/"+i
            if os.path.splitext(i)[-1]==".jpg":
                c+=1
                xwj=a+"/"+x+str(c)+".jpg"
                shutil.copyfile(ywj,xwj)
#代码结束
ml=os.listdir(lj+"/风景图片")
ml.sort()
for x in ml:
    print(x)

第5关 选择题

  • 1、若a.txt文件已经存在,并已经有多行文本 下列哪个语句可以打开a.txt,并在文本后追加helloworld  ( A )

    A、

    f1=open("a.txt","a") f1.write("helloworld") f1.close()

    B、

    f1=open("a.txt","w") f1.write("helloworld") f1.close()

    C、

    f1=open("a.txt","r") f1.write("helloworld") f1.close()

    D、

    f1=open("a.txt","x") f1.write("helloworld") f1.close()

  • 2、os库中建立文件夹的命令是 ( B )

    A、

    rmdir

    B、

    mkdir

    C、

    chdir

    D、

    listdir

  • 3、文件dat.txt里的内容如下:

    QQ&Wechat&Google &Baidu

    以下程序的输出结果是:( D )

    fo = open("tet.txt",'r')

    fo.seek(2)

    print(fo.read(7))

    fo.close()    
    A、Wechat
    B、QQ&Wech
    C、Wechat&
    D、&Wechat

  • 4、若需要在当前文件夹的上级文件夹下建立一个a.txt文件(此文件目前不存在) 将26个字母写入文件 以下正确的命令是  ( C )

    A、f1=open("..\\a.txt","w")

          f1.write("abcdefghijklmnopqrstuvwxyz")

     

    B、f1=open("..\\a.txt")

          f1.write("abcdefghijklmnopqrstuvwxyz")

          f1.close()

    C、f1=open("../a.txt","a")
      f1.write("abcdefghijklmnopqrstuvwxyz")
      f1.close()

     

    D、f1=open("a.txt","x")

          f1.write("abcdefghijklmnopqrstuvwxyz")

         f1.close()

  • 5、若需要将当前文件夹下的image文件夹(所有文件和子文件夹),复制到d盘的2021文件夹下,应该使用下列哪组命令  ( D )

  • A、import os

          os.system("copy images\\*.* d:\\2021")
    B、import os

          os.system("move images\\*.* d:\\2021")
    C、import os

      shutil.copyfile('images' ,'c:/2021')
    D、import os

      shutil.copytree('images' ,'c:/2021/images')

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]