#19、有一个ip.txt,里面每行是一个ip,实现一个函数,ping 每个ip的结果,把结果记录存到ping.txt中,格式为ip:0或ip:1 ,0代表ping成功,1代表ping失败 ''' def ping_ip(source_dir): import os with open(source_dir,"r") as fp: ip_list =fp.readlines() result =[] for ip in ip_list: if os.system("ping"+" "+ip)==0: result.append(ip.strip()+":"+"0"+" ") elif os.system("ping"+" "+ip)==1: result.append(ip.strip()+":"+"1"+" ") with open("C:\Users\dell\Desktop\练习\5\ping.txt","w",encoding="utf-8") as fp: fp.write("".join(result)) return result print(ping_ip("C:\Users\dell\Desktop\练习\5\ip.txt")) ''' #20、实现DOS命令执行功能,接受输入命令并执行,然后把执行结果和返回码打印到屏幕 #用while循环接受DOS命令 #os.system执行命令 ''' def get_command(command): import os result =os.system(command) if result ==0: print("执行成功") elif result==1: print("执行失败!") return result while 1: command =input("请输入要执行的命令:") if command =="exit": break else: get_command(command) ''' #21、文件访问访问一存在多行的文件,实现每隔一秒逐行显示文本内容的程序,每次显示文本文件的 5行, #暂停并向用户提示“输入任意字符继续”,按回车键后继续执行,直到文件末尾。 ''' import os import os.path import time def read_file(source_file): import os with open(source_file,"r",encoding="utf-8") as fp: content=fp.readlines() count =1 for line in content: time.sleep(1) if count%5==0: data =input("输入任意字符继续:") print(time.strftime("%Y-%m-%d %h:%M:%S",time.localtime())+line) count +=1 print(read_file("C:\Users\dell\Desktop\练习\5\0530.py")) ''' #习题1:输入一行字符,分别统计出其中的数字、字母、空行和其他字符的个数; ''' string_data=input("请输入一行字符串:") digit_num =0 letter_num =0 space_num =0 other_num =0 for i in string_data: if i.isdigit(): digit_num +=1 elif i.isalpha(): letter_num +=1 elif i.isspace(): space_num +=1 else: other_num +=1 print(digit_num,letter_num,space_num,other_num) ''' #习题2:求 s= a+aa+aaa+aaaa+aa....aa的s的值,其中a是 一个数字 ''' def sum(a,n): if not isinstance(a,int): return None s=0 for i in range(1,n+1): s+=int(str(a)*i) return s print(sum(2,3)) ''' #习题3:打印文件a.txt的文件路径 ''' def find_file_path(source_path): import os import os.path return os.path.split(source_path)[0] print(find_file_path("C:\Users\dell\Desktop\练习\5\ip.txt")) ''' ''' import os result_list =[] def find_file(path): for root,dirs,files in os.walk(path,topdown=False): for file in files: if "ip.txt" in file: result_list.append(os.path.join(root,file)) return result_list print(find_file("C:\Users\dell\Desktop\练习\5\ip.txt")) ''' #习题4:求一个文件路径下所有文件的数量 ''' def count_file_num(path): import os import os.path os.chdir(path) file_count =0 for i in os.listdir(path): if os.path.isfile(path+"\"+i): file_count +=1 return file_count print(count_file_num("C:\Users\dellDesktop\练习\5")) ''' #习题5: 找出txt文件的个数: ''' def count_txt_file_num(path): import os import os.path os.chdir(path) txt_file_count =0 for i in os.listdir(path): if os.path.isfile(path+"\"+i) and os.path.splitext(path+"\"+i)[1]==".txt": txt_file_count +=1 return txt_file_count print(count_txt_file_num("C:\Users\dellDesktop\练习\5")) ''' #习题5:查看目录下的所有文件 ''' import os path ="C:\Users\dellDesktop\练习\5" os.chdir(path) for i in os.listdir(path): print(i) ''' #习题6:找出一个目录及子目录下的所有文件名字,不含后缀 ''' import os filename_list =[] for root,dirs,files in os.walk("C:\Users\dellDesktop\练习\5",topdown=False): for file in files: #print(file) filename_list.append(os.path.splitext(file)[0]) print(filename_list) ''' #习题7:输入一个字母判断是星期几,如果第一个字母相同就判断第二个字 ''' week_day = {"m":"星期一","tu":"星期二","w":"星期三","th":"星期四","f":"星期五","sa":"星期六","su":"星期日"} word = input('请输入星期首字母:') result ={} new_result ={} for key,value in week_day.items(): if key[0]==word: result[key]=value if len(result)>1: two_word = input('请输入星期第二个字母:') for key,value in result.items(): if key[1]==two_word: new_result[key]=value print(new_result) else: print(result) '''