首页 > 怎样遍历移除项目中的所有 .pyc 文件

怎样遍历移除项目中的所有 .pyc 文件

对项目进行打包,所有 .pyc 文件需要移除。
由于有的 .pyc 文件在子目录里,所有不知道该怎么遍历移除,求解?


windows 里面可以使用
del *.pyc /s /a /q


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;
}

find /home/app/ -name "*.pyc" -print | xargs -n1 rm -rf


Ubuntu下,我用的是:

find -iname "*.pyc" -exec rm -f {} \;

rm `find /dirpath -type f -name "*.pyc"`

find /path -type f -name "*.pyc" -delete


Python打包? 如果是用的egg方式, 应该不需要去删除的吧


del *.pyc /s /a /q


如果你使用的是linux或者mac,在终端执行这样的操作

find /tmp -name "*.pyc" | xargs rm -rf

如果提示Permission denied,则使用

sudo find /tmp -name "*.pyc" | xargs rm -rf

将上面的/tmp替换成工程目录即可。这个命令会遍历删除工程目录(含子目录)下的pyc文件。


在打包时忽略 .pyc 文件或许是个更方便的办法。
tarzip都可以加上 --exclude=*.pyc 参数来排除 pyc 文件


rm **/*.pyc

补充说明:在bash下要在~/.bashrc中添加shopt -s globstar


如果是硬删除,使用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)

find . -type f -name "*.py[co]" -delete

提交到 github 的时候 .ignore
github 有zip下载
呵呵,折叠我.


新建一个.gitignore,内容

*.pyc

执行

git add -A
git commit -m 'blalala'

到别的目录克隆一下,pyc文件就没了

【热门文章】
【热门文章】