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


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

python shell bash

lamZx3 11 years, 3 months ago

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

石更不起来君 answered 11 years, 3 months ago

rm **/*.pyc

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

auxox answered 11 years, 3 months ago

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

梦中的少年 answered 11 years, 3 months ago


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

老湿不给力阿 answered 11 years, 3 months ago

新建一个 .gitignore ,内容


 *.pyc

执行


 git add -A
git commit -m 'blalala'

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

八重雾中渡 answered 11 years, 3 months ago

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

ariahsh answered 11 years, 3 months ago

del *.pyc /s /a /q

无尽的拍拍拍 answered 11 years, 3 months ago

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

kbmgs answered 11 years, 3 months ago


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

寂静D旋律〤 answered 11 years, 3 months ago

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

俊程Jared answered 11 years, 3 months ago

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

自豪的小吴克 answered 11 years, 3 months ago

Ubuntu下,我用的是:


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

氪金眼照样瞎 answered 11 years, 3 months ago

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


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

如果提示 Permission denied ,则使用


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

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

国士无双人渣诚 answered 11 years, 3 months ago

Your Answer