Notes learning python "python programming quick start - let automate the tedious work of" Eight

Selective Copy Write a program to traverse a directory tree to find a specific file extensions (such as .pdf or .jpg). No matter where the location of these files, copy them to a new folder.

import os
import shutil
def find_file(end_str=r'.txt',file_path=r'c:',new_path=r'd:'):
	if os.path.exists(new_path):
		pass
	else:
	#不存在则建立文件夹
		os.makedirs(new_path)
		#遍历文件夹
	for file_p , _ , file in os.walk(file_path):
		for i in file:
			if i.endswith(end_str):
				shutil.copy(os.path.join(file_p,i),new_path)
				

Delete unnecessary files
that do not require a huge file or folder occupies the space of the hard disk, which is not uncommon. If you're trying to release space on your computer, then delete the unwanted effects of huge files best. But first you have to find them. Write a program to traverse a directory tree to find a particularly large file or folder, for example, more than 100MB of files (Recall that the size of the file you want to get, you can use the os module os.path.getsize (). These documents the absolute path of the print to the screen.

def move_size(file_path,maxsize=100):
	for file_p , _ , file in os.walk(file_path):
		for i in file:
			if os.path.getsize(os.path.join(file_p,i))>=100*1024*1024:
				# print(i)
				flag=input('{}是否删除(y/n):'.format(i))
				if flag=='y':
					os.unlink(os.path.join(file_path,i))
				else:
					pass
Published 23 original articles · won praise 5 · Views 386

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/104484913