用不同语言来遍历目录和文件

最近翻移动硬盘,发现自己还短暂的写过两个月的perl语言,盘中赫然躺着一些perl脚本,其中一个便是实现的遍历目录及其包含的文件的脚本。
该功能无非就是利用递归来写代码,这里想用perl、python、shell和C语言分别对其进行编写,聊发少年狂而已。

1、程序列表

1
2
3
4
5
6
7
8
[root@localhost script]# ls -tlr
total 28
-rwxr--r--. 1 root root 276 May 17 19:16 shell_find.sh
-rwxr--r--. 1 root root 444 May 17 19:17 python_find.py
-rwxr--r--. 1 root root 782 May 17 19:18 perl_find.sh
-rw-r--r--. 1 root root 1184 May 17 19:36 C_Find.c
-rwxr-xr-x. 1 root root 9048 May 17 19:37 C_Find
[root@localhost script]#

1.1 perl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/perl

use strict;

my @filelist;
my $dir = "../python";

push(@filelist,&findfile($dir));
print join("\n",@filelist);
print "\n";

sub findfile{
my @filelist;
my $dir = $_[0];
opendir(DIR,$dir);
my @temp = grep(!/^\.\.?$/,readdir(DIR));
close(DIR);
my $count = @temp;
my $i;
my $tmpdir;
for($i = 0; $i < $count; $i++){
$tmpdir = ();
$tmpdir = "$dir/$temp[$i]";
if(-d "$tmpdir"){
push(@filelist,$tmpdir);
push(@filelist,&findfile("$tmpdir"));
}
else {
push(@filelist,$tmpdir);
}
}
return @filelist;
}

1.2 python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/python3
# coding=utf-8

import os
basedir = '../python'
filelists = []

def find(basedir):
global filelists
for parent,dirnames,filenames in os.walk(basedir):
for dirname in dirnames:
filelists.append(os.path.join(parent,dirname))
for filename in filenames:
filelists.append(os.path.join(parent,filename))

if __name__ == '__main__':
find(basedir)
print('\n'.join(filelists))

1.3 shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/bash

set -o nounset
set -o errexit

function find(){
for file in `ls $1`
do
if [ -d $1"/"$file ]
then
echo $1"/"$file
find $1"/"$file
else
echo $1"/"$file
fi
done
}

find "../python"

1.4 C

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* list dir and files through C */                                                                                                                                                           

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

#define MAX_PATH_LENGTH 512

int find(char *path)
{
DIR *ptr_dir;
struct dirent *dir_entry;

int i = 0;
char *child_path;

child_path = (char*)malloc(sizeof(char)*MAX_PATH_LENGTH);
if(child_path == NULL){
printf("malloc child_path occurs errors.\n");
exit(1);
}

memset(child_path,0x0,sizeof(char)*MAX_PATH_LENGTH);

ptr_dir = opendir(path);
while(( dir_entry = readdir(ptr_dir) ) != NULL){
if(dir_entry->d_type & DT_DIR){
if(strcmp(dir_entry->d_name,".") == 0 ||
strcmp(dir_entry->d_name,"..") == 0){
continue;
}
sprintf(child_path,"%s/%s",path,dir_entry->d_name);
printf("%s\n",child_path);

find(child_path);
} else {
printf("%s/%s\n",path,dir_entry->d_name);
}
}

free(child_path);
child_path = NULL;

return 0;
}
int main()
{
find("../python");

return 0;
}

编译:

1
gcc -o C_Find C_Find.c

2、执行结果

上述代码执行结果一致,输出文件列表的默认排序或有差别。
以shell脚本的执行结果为例,输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost script]# ./shell_find.sh 
../python/countCodeLine2.py
../python/countCodeLine.py
../python/countCodeLine.py_ori
../python/Django
../python/How_to_be_pythonic
../python/How_to_be_pythonic/add.c
../python/How_to_be_pythonic/adder.so
../python/How_to_be_pythonic/app_decrator.py
../python/How_to_be_pythonic/cpython_api.py
../python/How_to_be_pythonic/cpython_ctype.py
../python/How_to_be_pythonic/cpython_SWIG.py
../python/How_to_be_pythonic/debug.txt
../python/How_to_be_pythonic/decrator.py
../python/How_to_be_pythonic/def__.py
../python/How_to_be_pythonic/enumerate.py
../python/How_to_be_pythonic/exception.py
../python/How_to_be_pythonic/forelse.py
../python/How_to_be_pythonic/funccode1.py
../python/How_to_be_pythonic/Generators.py
../python/How_to_be_pythonic/kw-args.py
../python/How_to_be_pythonic/out.log
../python/How_to_be_pythonic/pprint.py
../python/How_to_be_pythonic/set.py
../python/How_to_be_pythonic/test3.py
../python/How_to_be_pythonic/test.py
[root@localhost script]#

3、后记

  1. C语言的实现方法中,有关dirent、stat等结构体含义的描述,可参见:
    http://www.360doc.com/content/15/0701/10/5470123_481878714.shtml
  2. 代码中输出的是目录及文件,可调整。
  3. 对列出的文件可做进一步的条件筛选,如后缀判断等。
  4. 可比较时间,可优化。
  5. 本文原作者:不出名的刀客,转载请一同转发本文链接。
  6. 第一次网上写技术文,文有不对之处,欢迎指正。
  7. 另,小白正在学习python,欢迎大神留言指教。
觉得文章不错的话,也可以支持一下哦~
-------------本文结束感谢您的阅读-------------
0%