#224、创建一个目录,年月日,规则如下:
#创建一个当前年份的目录
#在该年份的目录下创建月份的目录
#在月份的目录下创建日的目录
#然后在日目录下,创建一个文件,当前小时为文件名,然后文件内容写上年月日时分秒
import time
import os
try:
os.makedirs("d:\\"+str(time.localtime()[0])+"\\"+\
str(time.localtime()[1])+"\\"+str(time.localtime()[2])+"\\")
except Exception as e:
print(e)
os.chdir("d:\\"+str(time.localtime()[0])+"\\"+\
str(time.localtime()[1])+"\\"+str(time.localtime()[2])+"\\")
with open(str(time.localtime()[3])+".txt","w") as fp:
content=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
fp.write(content)
#225、在e盘下新建指定深度的文件名,规则如下:
#传入参数2,目录名为gloryroad,文件名为a.txt
#则会生成e:\gloryroad\gloryroad\a.txt
import os
def create_dir(deep,dir_name,file_name):
path="d:\\"
for i in range(deep):
path+=(dir_name+"\\")
print(path)
try:
os.makedirs(path)
except Exception as e:
print(e)
with open(path+"a.txt","w") as fp:
print("已创建"+file_name)
create_dir(4,"gloryroad","a.txt")
#226、写一个函数,读取文件指定行数的内容,如前5行,第3行到第6行等
import os
def read_file(path,start=None,end=None):
if not os.path.exists(path):
print("文件不存在")
else:
with open(path) as fp:
lines=fp.readlines()
if start is None:
start=0
if end is None:
end=len(lines)
if end>len(lines):
end=len(lines)
for line in range(start,end):
print(lines[line])
#read_file("d:\\2019\\1.txt",start=10)
#read_file("d:\\2019\\1.txt",end=10)
#read_file("d:\\2019\\1.txt")
read_file("d:\\2019\\1.txt",start=10,end=80)
#227、在e盘下新建如下目录文件结构,找出python_practice目录下所有的文件,
#并在每个文件中写入”i am learning gloryroad!”
import os
for root,dir,file in os.walk("d:\\python_practice"):
print(root,dir,file)
for i in file:
with open(root+os.sep+i,"w") as fp:
fp.write("I am a girl")
#228、文件内容为如下诗词,请将每行倒序输出并重新写入该文件
#春花秋月何时了?往事知多少。小楼昨夜又东风,故国不堪回首月明中。
#雕栏玉砌应犹在,只是朱颜改。问君能有几多愁?恰似一江春水向东流。
#倒序重新写入的效果为:
#小楼昨夜又东风,故国不堪回首月明中。春花秋月何时了?往事知多少。
#问君能有几多愁?恰似一江春水向东流。雕栏玉砌应犹在,只是朱颜改
new_line=[]
with open("d:\\2019\\1.txt") as fp:
for line in fp:
line_list=line.split("。")
new_line_list= line_list[:]
new_line_list[0]=line_list[1]
new_line_list[1]=line_list[0]
new_line_list[2]=line_list[2]
print("。".join(new_line_list))
new_line.append("。".join(new_line_list))
with open("d:\\2019\\1.txt","w") as fp:
fp.writelines(new_line)
#229、文件内容为一个多层元组,遍历该元组,当全为数字时输出数字之和,
#全为字母输出字符串,有数字有字母输出False,
#并将该内容写入到该文件的下一行中
#如文件a.txt内容为:((1,2,3),(“a”,“b”,“c”),(“a”,1,2))
#则执行该程序后,文件a.txt内容将会如下:有两行,第一行文件原来的内容,第二行是按规则生成的一组元组数据
#((1,2,3),(“a”,“b”,“c”),(“a”,1,2))
#(6,“abc”,False)
def is_all_digit(tup):
if not isinstance(tup,tuple):
return False
for i in tup:
if not isinstance(i,(int,float)):
return False
return True
def is_all_letter(tup):
if not isinstance(tup,tuple):
return False
for i in tup:
if not str(i).isalpha():
return False
return True
result=[]
with open("d:\\2019\\1.txt","r") as fp:
content=fp.read()
content=eval(content)
print(content,type(content))
digit_sum=0
letter_join=""
for tup in content:
if is_all_digit(tup):
for i in tup:
digit_sum+=i
result.append(digit_sum)
elif is_all_letter(tup):
letter_join="".join(tup)
result.append(letter_join)
else:
result.append(False)
print(result)
write_content=tuple(result)
with open("d:\\2019\\1.txt","a") as fp:
fp.write("\n"+str(write_content))