对项目进行打包,所有 .pyc 文件需要移除。
由于有的 .pyc 文件在子目录里,所有不知道该怎么遍历移除,求解?
windows可以用这个,另存为delete_file.js。
linux下根本不用这么费事,一个rm就搞定了。。。
修改要删除的文件后缀名file_end
/// Global ----------------------------------------------------
var file_end = "pyc";
var consoleOnly = true;
var defaultTimeout = 1;
var WSShell;
var fso;
var currentFolder;
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var CharSetDefault = -2, CharSetUnicode = -1, CharSetAscii = 0;
var AttrNormal = 0, AttrReadOnly = 1, AttrHidden = 2, AttrSystem = 4,
AttrVolume = 8, AttrDirectory = 16, AttrArchive = 32, AttrAlias = 1024,
AttrCompressed = 2048;
Init();
Main();
function Init() {
// detect command line
try {
WScript.StdOut.WriteLine(" ");
} catch (e) {
consoleOnly = false;
}
// initialize
WSShell = new ActiveXObject("WScript.Shell");
fso = new ActiveXObject("Scripting.FileSystemObject");
currentFolder = GetCurrentFolder();
}
function Main() {
var text;
text = "开始处理.";
Out(text, true);
COut("当前工作目录: \r\n" + currentFolder);
var files = FindFiles("[.]*[.]" + file_end);
COut("共有 " + files.length + " 个 ." + file_end + " 文件");
var deleted = 0;
for(var i=0;i<files.length;i++){
var f = fso.GetFile(files[i]);
f.Delete(true);
deleted++;
}
Out("成功删除了 " + deleted + " 个 ." + file_end + " 文件", false);
}
/// Files ------------------------------------------------------
// getcurrent folder
function GetCurrentFolder() {
return fso.GetFolder(fso.GetFile(WScript.ScriptFullName).ParentFolder);
}
/// Output ------------------------------------------------------
// output
function Out(text, useTimeout) {
if (useTimeout) {
useTimeout = defaultTimeout;
} else {
useTimeout = -1;
}
if (consoleOnly) {
WScript.StdOut.WriteLine(text);
} else {
WSShell.Popup(text, useTimeout, "删除 ." + file_end + " 文件");
}
}
// output
function COut(text, useTimeout) {
if (useTimeout) {
useTimeout = defaultTimeout;
} else {
useTimeout = -1;
}
if (consoleOnly) {
WScript.StdOut.WriteLine(text);
}
}
function ReadFile(file) {
var stream = file.OpenAsTextStream(ForReading, CharSetDefault);
text = stream.ReadAll();
stream.Close();
return text;
}
function WriteFile(file, text) {
var ro = ((file.Attributes & AttrReadOnly) != 0);
if (ro) file.Attributes -= AttrReadOnly;
var stream = file.OpenAsTextStream(ForWriting, CharSetDefault);
stream.Write(text);
stream.Close();
if (ro) file.Attributes += AttrReadOnly;
}
// determine, if filename matches given mask
function MatchesMask(file, mask) {
return new RegExp(mask).test(file);
}
// find files
function FindFiles(mask) {
return GetFiles(currentFolder, mask);
}
// get files in current folder & subfolders
function GetFiles(folder, mask) {
var result = new Array();
// do files in current folder
var files = new Enumerator(folder.Files);
for (; !files.atEnd(); files.moveNext()) {
if (MatchesMask(files.item(), mask)) {
result.push("" + files.item());
}
}
// do subfolders in current folder
var folders = new Enumerator(folder.SubFolders);
for (; !folders.atEnd(); folders.moveNext()) {
result = result.concat(GetFiles(folders.item(), mask));
}
return result;
}
如果是硬删除,使用Python脚本来解决:
import os
path = 'project-path'
for prefix, dirs, files in os.walk(path):
for name in files:
if name.endswith('.pyc'):
filename = os.path.join(prefix, name)
os.remove(filename)
1 回答9.6k 阅读✓ 已解决
2 回答5.3k 阅读✓ 已解决
2 回答3.6k 阅读✓ 已解决
3 回答4.5k 阅读
3 回答1.4k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
2 回答1.6k 阅读✓ 已解决
如果你使用的是linux或者mac,在终端执行这样的操作
如果提示Permission denied,则使用
将上面的/tmp替换成工程目录即可。这个命令会遍历删除工程目录(含子目录)下的pyc文件。