测试题:
0.编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图:
>>>=================================RESTART==============================
该文件夹下共有类型为【.txt】的文件1个
该文件夹下共有类型为【.png】的文件2个
该文件夹下共有类型为【.py】的文件3个
该文件夹下共有类型为【.docx】的文件2个
该文件夹下共有类型为【文件夹】的文件2个
>>>
import os
list_ext=[]
for each in os.listdir('.'):
extension = os.path.splitext(each)[1]
list_ext.append(extension)
set_list_ext = set(list_ext)
for each in set_list_ext:
if each is not '':
print('该文件下共有类型为【%s】的文件%d个'%(str(each),list_ext.count(each)))
else:
print('该文件下共有类型为【文件夹】的文件%d个'%list_ext.count(each))
def file_size(path):
os.chdir(path)
all_file = os.listdir('.')
for each in all_file:
print('%s 【%dBytes】' %(each,os.path.getsize(each)))
path = input('请输入路径:')
file_size(path)
import os
def search_file(start_dir,target):
os.chdir(start_dir)
for each_file in os.listdir(os.curdir):
if each_file == target:
print(os.getcwd()+os.sep+each_file) #使用os.sep使程序更标准
if os.path.isdir(each_file):
search_file(each_file,target) #递归调用
os.chdir(os.pardir) #递归调用后切记返回上一层目录
start_dir = input('请输入待查找的初始目录:')
target = input('请输入需要查找的目标文件:')
search_file(start_dir,target)
import os
f=open(os.curdir+os.sep+'vedioList.txt','w')
def vediolist(path,target):
os.chdir(path)
all_file = os.listdir(os.curdir)
for each_file in all_file:
ext = os.path.splitext(each_file)[1]
if ext in target:
f.write(os.getcwd()+os.sep+each_file+os.linesep)
else:
pass
if os.path.isdir(each_file):
vediolist(each_file,target)
os.chdir(os.pardir)
path = input('请输入待查找的初始目录:')
target = ['.mp4','.avi','.rmvb','.mkv']
vediolist(path,target)
f.close()
import os
#word_dict=dict()
def search_kw(path,kw):
os.chdir(path)
all_file = os.listdir(os.curdir)
for each_file in all_file:
#each_file+'_dict'=dict()
if os.path.splitext(each_file)[1] == '.txt':
print('在文件%s中\n'%each_file)
f = open(each_file,'r')
string=f.readlines()
count = 0
for each_line in string:
count+=1
pos = []
begin = each_line.find(kw)
while begin != -1:
pos.append(begin+1)
begin = each_line.find(kw,begin+1)
print('关键字出现在第%d行,第%s个位置'%(count,str(pos)))
if os.path.isdir(each_file):
search_kw(each_file,kw)
os.chdir(os.pardir)
path = input('请输入文件路径:')
kw=input('请将该脚本放于待查找的文件夹内,请输入关键字:')
search_kw(path,kw)
答案:
import os
def print_pos(key_dict):
keys = key_dict.keys()
keys = sorted(keys)
for each_key in keys:
print('关键字出现在第%d行,第%s个位置' %(each_key,str(key_dict[each_key])))
def pos_in_line(line,key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin +1) #用户的角度是从1开始数
begin = line.find(key,begin+1) #从下一位置继续查找
return pos
def search_in_file(file_name,key):
f = open(file_name)
count = 0 #记录行数
key_dict = dict() #字典,用户存放key坐在具体行数对应的位置
for each_line in f:
count +=1
if key in each_line:
pos = pos_in_line(each_line,key) #key在每行对应的位置
key_dict[count] =pos
f.close()
return key_dict
def search_files(key,detail):
all_files = os.walk(os.getcwd())
txt_files = []
for i in all_files:
for each_file in i[2]:
if os.path.splitext(each_file)[1] == '.txt': #根据后缀判断是否文本文件
each_file = os.path.join(i[0],each_file)
txt_files.append(each_file)
for each_txt_file in txt_files:
key_dict = search_in_file(each_txt_file,key)
if key_dict:
print('=================================================')
print('在文件【%s】中找到关键字【%s】' %(each_txt_file,key))
if detail in ['YES','Yes','yes']:
print_pos(key_dict)
key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)
search_files(key,detail)