Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文件夹大小报告到CSV_Python_Perl_Bash_Csv_Du - Fatal编程技术网

Python 文件夹大小报告到CSV

Python 文件夹大小报告到CSV,python,perl,bash,csv,du,Python,Perl,Bash,Csv,Du,我想我到处都找过了,运气不太好。我正在尝试创建一个简单的自动计费报告。我需要创建一个包含3列的CSV。像这样 Folder Size Billing Code Folder1 300M XXXXX Folder2 600M XXXXX Folder3 200M XXXXX 我可以得到一个du-sh并使用awk来安排我想要做的事情,我得到了我想要做的大部分事情。但是,我不知道如何在每列之前创建标题。我在bash、perl和python中寻找了一些方法,但成功有限。如果我

我想我到处都找过了,运气不太好。我正在尝试创建一个简单的自动计费报告。我需要创建一个包含3列的CSV。像这样

Folder   Size   Billing Code
Folder1  300M   XXXXX
Folder2  600M   XXXXX
Folder3  200M   XXXXX
我可以得到一个
du-sh
并使用
awk
来安排我想要做的事情,我得到了我想要做的大部分事情。但是,我不知道如何在每列之前创建标题。我在bash、perl和python中寻找了一些方法,但成功有限。如果我能弄清楚如何制作列标题标签,我想我可以从那里开始了。

试试Python中的。具体来说,您需要writeheader()方法,它非常简单


或者,如果已经在shell脚本中执行此操作,为什么不首先将头行回显到文件,然后确保后续命令使用append操作符(>>)?

在渲染值后添加名称呢

$ sed 1i"This is the first line" - < /proc/loadavg
This is the first line
3.14 3.48 3.58 2/533 17661
$sed 1i“这是第一行”-
使用perl的方法:

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

open my $fh, '>:encoding(utf8)','your outfile.csv';

# write the first line with the column names
print $fh "Folder,Size,Billing Code\n";

# Do your folder calculations
my $dirpath = "/your/path";
my $bytes;
find(\&folders,$dirpath);
close $fh;

sub folders {
    if (-d $_) {
        $bytes = 0;
        find(\&size,$_);
        $bytes = $bytes/1_048_576; # Convert Bytes to Mega Bytes
        print $fh $_.",".$bytes."M,your_billing_code \n";
    }
}

sub size {
    if(-f $_){
        $bytes += -s _; # This is not a typo
                        # -X _ does a filesystem operation on the 
                        # last used stat structure
                        # this saves new filesystem calls and therefore performance
                        # see http://perldoc.perl.org/functions/-X.html for more info
    }
}
这在没有外部命令(如du)的情况下写入CSV文件,并且可以在每个平台上工作。
还要检查Perl的CSV模块以获得更多选项

Hmm,破折号可能是多余的,但显然它也可以使用(: