赞
踩
import os def mkDir(): # 创建名为 'dst' 的文件夹 os.makedirs('dst', exist_ok=True) # 如果文件被直接运行,执行测试代码 if __name__ == "__main__": mkDir() # 判断当前文件夹中是否存在文件夹 'dst' if os.path.exists('dst') and os.path.isdir('dst'): print('mkdir success') else: print('dst does not exist')
import os
import sys
def removeFile():
# 删除 'src/removeme.txt' 文件
try:
os.remove('src/removeme.txt')
print('remove success')
except FileNotFoundError:
print('remove success')
sys.exit()
# 如果文件被直接运行,执行测试代码
if __name__ == "__main__":
removeFile()
未通过本题,如果您通过了本题欢迎补充到评论区,有时间我会整理进来
import shutil import os if os.path.exists("wjcl/src/step3/cr"): shutil.rmtree("wjcl/src/step3/cr") os.mkdir("wjcl/src/step3/cr") f1=open("wjcl/src/step1/宋词.txt",'r') #代码开始 #os.chdir("wjcl/src/step3/cr") for line in f1.readlines():#逐行读取文件 k=0 if " "in line: k=1 pos=line.find(" ")#pos为冒号所在位置 pos1=line.find("\n") xm=line[pos+1:pos1]#截取姓名 if xm !="": f2=open('wjcl/src/step3/cr/'+xm+".txt",'a+')#以追加的方式打开文件xm.txt(不存在就新建,存在就打开) if len(line.strip("\n").strip())>0: f2.write(line)#将读出的内容写入文件 if len(line.strip("\n").strip()) > 0 and k !=1: f2.write(line) # 将读出的内容写入文件 #代码结束 f1.close() f2.close()
import os import shutil if os.path.exists("wjcl/src/step4/sccr"): shutil.rmtree("wjcl/src/step4/sccr") os.mkdir("wjcl/src/step4/sccr") f1=open("wjcl/src/step1/宋词.txt",'r') #代码开始 for line in f1.readlines():#逐行读取文件 k=0 if " "in line: k=1 pos=line.find(" ")#pos为冒号所在位置 pos1=line.find("\n") xm=line[pos+1:pos1]#截取姓名 if not os.path.exists("wjcl/src/step4/sccr/"+xm):os.mkdir("wjcl/src/step4/sccr/"+xm)#以追加的方式打开文件xm.txt(不存在就新建,存在就打开) f1.close() #代码结束
import os import shutil if os.path.exists("wjcl/src/step5/cr"): shutil.rmtree("wjcl/src/step5/cr") os.mkdir("wjcl/src/step5/cr") f1=open("wjcl/src/step1/宋词.txt",'r') #代码开始 list_str = f1.readlines() temp_str = '' name = '' for s in list_str: if s.find(' ') != -1: if len(temp_str) == 0: temp_str += s else: if name == '': name = s file_path = 'wjcl/src/step5/cr/' + name[name.find(' ') + 1:-1] if not os.path.exists(file_path): os.mkdir(file_path) title_path = file_path + '/' + name[0:name.find(' ')] + '.txt' f2 = open(title_path, 'a+', encoding='utf-8') f2.write(temp_str) f2.close() temp_str = s name = s else: temp_str += s file_path = 'wjcl/src/step5/cr/' + name[name.find(' ') + 1:-1] if not os.path.exists(file_path): os.mkdir(file_path) title_path = file_path + '/' + name[0:name.find(' ')] + '.txt' f2 = open(title_path, 'a+', encoding='utf-8') f2.write(temp_str) f2.close() f1.close() #代码结束
import os wjzd={} wjsize={} lj="wjcl/src/test" #代码开始 def get_size_type(f_path): global lj files_name = os.listdir(f_path) for name in files_name: file_path = os.path.join(f_path, name) ty = str(os.path.splitext(file_path)[1]).strip('.') if os.path.isdir(file_path): get_size_type(file_path) if not ty or ty == 'gitkeep': continue else: wjzd.setdefault(ty, 0) wjzd[ty] += 1 wjsize.setdefault(ty, 0) wjsize[ty] += os.path.getsize(file_path) get_size_type(lj) ans = ['txt', 'mp3', 'jpg', 'pptx'] #代码结束 for x in ans: print("文件类型{}文件数{}文件大小{:.2f}KB".format(x,wjzd[x],wjsize[x]/1024))
import os import shutil wj={"图片":".jpeg.jpg.png.jfif","文档":".txt.docx.pdf","音乐":".mp3.wav","影像":".mp4.flv"} lj1="wjcl/src/test2" lj="wjcl/src/test3" if os.path.exists(lj): shutil.rmtree(lj) shutil.copytree(lj1,lj) #代码开始 def move_file(path): if not os.path.exists(lj + path): os.mkdir(lj + path) shutil.move(file_path, lj + path) files = os.listdir(lj) for name in files: file_path = os.path.join(lj, name) ty = str(os.path.splitext(name)[1]) if ty == '.gitkeep' or name == '.gitkeep': continue if wj['图片'].find(ty) != -1: move_file('/img') elif wj['文档'].find(ty) != -1: move_file('/idoc') elif wj['音乐'].find(ty) != -1: move_file('/music') elif wj['影像'].find(ty) != -1: move_file('/video') #代码结束 #for x in os.listdir(lj): # if x!=".gitkeep": # print(os.listdir(lj+"/"+x)) l = [['青城山.png', '杜甫草堂.png', '春熙路图集3.jfif', 'ifs大熊猫.jpg', '春熙路图集2.jpeg', '基地1.jpg', '春熙路图集1.jfif'], ['学院设置.docx', '昆明.docx', '考题四.pdf', '琵琶行并序.txt', '考题一.pdf'], ['pq.flv', '七彩丹霞视频.mp4', '云南十八怪_.mp4'], ['花儿尕恋手令.mp3', 'add.wav', 'score.wav', '山歌好比春江水.mp3']] for n in l: print(n) #代码结束
import csv #导入csv模块 ########## Begin ########## contents=[] filename='project/iris.csv' csv_file=open(filename,mode='r',encoding='utf-8') reader=csv.reader(csv_file) for item in reader: contents.append(item) #打开数据集的文件,file是相应的文件句柄 #读取数据,把数据放在contents中 ########## End ########## for line in contents: #打印文件内容 ########## Begin ########## print(line) ########## End ########## line_num=len(contents) print(line_num)
import csv ########## Begin ########## filename='project/students.csv' csvfile2=open(filename,'w',newline='') writer2=csv.writer(csvfile2) #打开文件,newline是否换行的参数 #如果不指定newline='',则每写入一行将有一空行被写入 ########## End ########## row=["序号","学号","姓名","性别","学院"] rows=[[1,"1409090312","张雨","女","计算机学院"], [2,"1409103265","陈敢","男","理学院"], [3,"1509111023","李家祥","女","计算机学院"], [4,"1409090311","邓贵","男","理学院"], ] ########## Begin ########## writer2.writerow(row) writer2.writerows(rows) row1=['1',"1409090312","张雨","女","计算机学院"] row2=['2',"1409103265","陈敢","男","理学院"] row3= ['3',"1509111023","李家祥","女","计算机学院"] row4=['4',"1409090311","邓贵","男","理学院"] #将row和rows写入文件 print(row) print(row1) print(row2) print(row3) print(row4,end='') # for item in rows # print(item) # filename='project/students.csv' # with open(filename,'r')as file: # csv_reader=csv.reader(file) # for row in csv_reader: # print(row) ########## End ##########
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。