The effect
The script you just wrote will automatically delete node_modules or other files from your computer.
Support configuration, you can specify directories for scanning, some old project dependencies to clear.
After a quick clean up, there are four extra G’s, which is quite comfortable.
The cause of
Computer capacity is relatively small, only 120G, often to clear the hard disk.
What cleans up more is the cache file of wechat. Today I found that the code also takes up a lot of space, usually write front-end and node.js slightly more, the code does not take up much capacity, so it should be the project image resources and node_modules problem.
I looked it up, and it was. Although many projects are old, it would be a shame to simply delete the entire code because of computer capacity. So it’s better to clean up these dependencies in bulk.
If one manually click to delete, more trouble. Even if you do it manually, you’ll have to do it manually again sometime later.
So it’s better to write a script and let the script do the work. And if you change the parameters, you can apply them to other scenarios next time, or add some preconditions, and you can do more.
code
import os
import time
import shutil
autodelete.py
# Script purpose: Automatically delete the related folders under the specified folder, such as batch delete node_modules dependency
def get_dirsize(dirPath):
size = 0
for root, dirs, files in os.walk(dirPath):
for file in files:
try:
name = os.path.join(root, file)
temp = os.path.getsize(name)
size += temp
except IOError:
error = "File exception"
return size / (1024*1024)
def auto_delete(file_dir,target,days):
sum_size = 0
for root, dirs, files in os.walk(file_dir):
Get all directories
for dir in dirs:
if dir == target:
abs_dir = os.path.join(root,dir)
There is no need to scan the child node_modules
if(abs_dir.count(target) > 1):
break
diff_time = (time.time() - os.path.getmtime(abs_dir))/60/60/24
size = get_dirsize(abs_dir)
if diff_time > days:
# This line is the code for clearing the folder. It is recommended to run the command to scan the folder first to ensure that the files can be deleted, and then uncomment.
# shutil.rmtree(abs_dir)
print("Deleted: %s"%(abs_dir))
print("Folder size: %.2fMB"%(size))
print("It has been %.2f days since the last update"%(diff_time))
print("-- -- -- -- -- -- -- --")
sum_size += size
print("%s folder not updated within %d days has been automatically deleted, saving %.2FMB space"%(days,target,sum_size))
Enter the directory address
root = "/Users/zengzhiqiang/Desktop"
Enter a folder name
target = "node_modules"
Node_modules will not be deleted for folders modified in the last 300 days
days = 300
auto_delete(root,target,days)
Copy the code
The overall logic is relatively simple, directly scan all files under the directory, find the corresponding folder, meet the given conditions, you can perform deletion.
Here the target=”node_modules” can also be changed to something else, such as “=logs”, to delete some log files.
Or target=”assets”,days=365, delete old resource files that have not been updated for a long time.
The last
This script, the code is relatively simple.
This article is just a brick for everyone, hoping to expand our thinking and diverge with each other. You might want to try writing a script if you encounter repetitive dot and dot problems in the future.
Of course, the most direct solution, in fact, is to change the computer, or Google, how to quickly empty XXX files.
Attach GitHub address: github.com/qiangzi7723…