| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
注意: 
1.需要用到字典,字典的用法需要多复习 
2.需要用到递归,递归的用法要积累 
 
- 动动手:
 
 - 0.
 
 - import os
 
  
- file_name = os.listdir('/Users/yufan/Documents/python编程/30/doc')
 
 - print(file_name)
 
  
- txt = 0
 
 - png = 0
 
 - py = 0
 
 - docx = 0
 
 - doc = 0
 
  
- for each_file in file_name:
 
 -     if '.txt' in each_file:
 
 -         txt += 1
 
 -     elif '.png' in each_file:
 
 -         png += 1
 
 -     elif '.py' in each_file:
 
 -         py += 1
 
 -     elif '.docx' in each_file:
 
 -         docx += 1
 
 -     elif '.' not in each_file:
 
 -         doc += 1
 
  
- print('txt 是 %d, png 是 %d, py 是 %d, docx 是 %d, doc 是 %d' % (txt,png,py,docx,doc))
 
  
- 1.
 
 - import os
 
 - doc_path = '/Users/yufan/Documents/python编程/30'
 
 - file_name = os.listdir(doc_path)
 
  
- file_path = []
 
  
- for each_file_name in file_name:
 
 -     file_path.append(os.path.join(doc_path,each_file_name))
 
  
- for each_file in file_path:
 
 -     print('文件 %s 的大小是 %d byte.' % (os.path.basename(each_file),os.path.getsize(each_file)))
 
  
- 2.
 
 - import os
 
  
- doc_path = input('请输入待查找的初始目录:')
 
 - while not os.path.isdir(doc_path):
 
 -     doc_path = input('没有这个路径,请重新输(输入exit退出):')
 
 -     if doc_path == 'exit':
 
 -         break
 
  
- file_name = input('请输入需要寻找的目标文件:')
 
  
- fold = os.walk(doc_path)
 
  
- flag = 0
 
  
- all_file = []
 
 - for each in fold:
 
 -     for each_file_name in each[2]:
 
 -         if file_name == each_file_name:
 
 -             print(each[0] +'/'+ file_name)
 
 -         else:
 
 -             flag = 1
 
 - if flag == 1:
 
 -     print('没有这个文件噢!')
 
  
- 3.
 
 - import os
 
  
- doc_path = input('请输入待查找的初始目录:')
 
 - while not os.path.isdir(doc_path):
 
 -     doc_path = input('输入错误,请重新输入:')
 
  
- doc_walk = os.walk(doc_path)
 
  
- file_path = []
 
  
- for each_part in doc_walk:
 
 -     for each_file in each_part[2]:
 
 -         if '.avi' or '.mp4' or '.rmvb' in each_file.lower():
 
 -             file_path.append(os.path.join(each_part[0],each_file) + '\n' * 2)
 
  
- print(file_path)
 
  
- f = open('/Users/yufan/Documents/python编程/30sp/videoList.txt','w',encoding = 'GBK')
 
 - f.writelines(file_path)
 
 - f.close()
 
  
- 4.
 
 - import os
 
  
- keyword = input('请将该代码放于待查找的文件夹内,请输入关键字:')
 
 - allow = input('请问是否需要打印关键字 *%s* 在文件夹中的位置(yes/no):' % keyword)
 
 - while allow.lower() != 'yes':
 
 -     allow = input('逗你的,输入yes吧:')
 
  
- cur_doc_path = os.getcwd()
 
  
- all_file = os.walk(cur_doc_path)
 
  
- txt_path = []
 
  
- for each_part in all_file:
 
 -     for each_file in each_part[2]:
 
 -         if '.txt' in each_file:
 
 -             txt_path.append(os.path.join(each_part[0],each_file))
 
  
- for each_path in txt_path:
 
 -     count = 1
 
 -     line = []
 
 -     position = []
 
 -     f = open(each_path,encoding = 'GBK')
 
 -     for each_line in f:
 
 -         line_position = []
 
 -         if keyword in each_line:
 
 -             line.append(count)
 
 -             for each in range(len(each_line)):
 
 -                 if keyword[0] == each_line[each]:
 
 -                     line_position.append(each+1)
 
 -             position.append(line_position)
 
 -         count += 1
 
 -     print('在文件*%s*中找到关键字*%s*' % (each_path,keyword))
 
 -     for each in range(len(line)):
 
 -         print('关键字出现在第 %d 行,第 ' % line[each],position[each],'个位置')
 
  复制代码 |   
 
 
 
 |