设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【每日一坑 4】 查找文件

[复制链接]

0

主题

0

好友

34

积分

新手上路

Rank: 1

15#
发表于 2014-11-25 10:50:04 |只看该作者
[i for i in test]这是一种生成list的方法,通过后面的if可以增加生成时的过滤条件。这种写法在python中很常用。
test > text ,写错了
回复

使用道具 举报

1

主题

0

好友

50

积分

注册会员

Rank: 2

14#
发表于 2014-6-26 23:56:21 |只看该作者
  1. #!/usr/bin/env python
  2. #coding:UTF-8

  3. import os
  4. def VisitDir(path):
  5.   for root,dirs,files in os.walk(path):
  6.       for f in files:
  7.           if os.path.splitext(f)[1]=='.exe':
  8.               print f
  9.          
  10. if __name__=="__main__":
  11.     path="."
  12.     VisitDir(path)
复制代码
回复

使用道具 举报

0

主题

0

好友

26

积分

新手上路

Rank: 1

13#
发表于 2014-1-18 10:32:45 |只看该作者
  1. import os
  2. from os.path import join

  3. the_directory = raw_input('Please enter the directory\n')
  4. base_directory = os.path.basename(the_directory)
  5. os.chdir(os.path.dirname(the_directory))
  6. all_files = []
  7. for root, dirs, files in os.walk(base_directory):
  8.     for name in files:
  9.         all_files.append(join(root, name))
  10. for i in all_files:
  11.     if '.txt' in i:
  12.         print i
复制代码
回复

使用道具 举报

0

主题

0

好友

49

积分

新手上路

Rank: 1

12#
发表于 2013-12-26 02:59:49 |只看该作者
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. import os, fnmatch

  4. def Findfiles(path, ext):
  5.         print 'Finding %s in %s' %(ext, path)
  6.         for root, dirs, files in os.walk(path):
  7.                 for file in files:
  8.                         if fnmatch.fnmatch(file, ext):
  9.                                 print 'Found %s in %s' %(file, root)


  10. print "\n\next format '*.jpg'\n\n"
  11. path = raw_input('Path: ')
  12. ext = raw_input('Ext: ')

  13. Findfiles(path, ext)
复制代码
回复

使用道具 举报

0

主题

0

好友

4

积分

新手上路

Rank: 1

11#
发表于 2013-12-24 23:21:23 |只看该作者
  1. import sys                                                                  
  2. import os

  3. if len(sys.argv) != 2:
  4.     print 'Please input dirname'
  5.     sys.exit(1)

  6. dirname = sys.argv[1]
  7. for root, _dir, files in os.walk(dirname):
  8.     for file in files:
  9.         if file.endswith('.txt'):
  10.             print os.path.join(root, file)
复制代码
回复

使用道具 举报

0

主题

0

好友

134

积分

注册会员

Rank: 2

10#
发表于 2013-12-21 16:57:58 |只看该作者
  1. import os,fnmatch
  2. def find_txt(root,patterns='*.txt'):#定义主函数,接受目录与所要匹配的拓展名为参数
  3.     list_of_txt=[]#列表储存txt文件名
  4.     for dirname,subdir,filenames in os.walk(root):#使用os。walk(遍历包括子目录下所有文件),生成三组列表,第一组为当前目录地址,第二组为当下前目录下的文件夹名(没有则返回一个空列表),第三组为当前目录的所有文件
  5.         for name in filenames:#遍历每组文件名
  6.             if fnmatch.fnmatch(name,patterns):#用fnmatch。fnmatch寻找匹配的文件
  7.                 list_of_txt.append(name)#加入列表
  8.     return list_of_txt
  9. if __name__=='__main__':
  10.     dir=raw_input('directory:')
  11.     txtfiles=find_txt(dir)
  12.     print txtfiles
复制代码
回复

使用道具 举报

0

主题

0

好友

79

积分

注册会员

Rank: 2

9#
发表于 2013-12-21 01:59:29 |只看该作者
长长的c又来了,有点乱没来得急整理。。。
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <sys/stat.h>

  8. #define PATH_SIZE 4096

  9. char *path;

  10. int show_txt(char *);

  11. int main(int argc, const char *argv[])
  12. {
  13.   if (argc != 2) {
  14.     printf("Usage: ./a.out <path of dir>\n");
  15.     exit(1);
  16.   }

  17.   path = malloc(PATH_SIZE);
  18.   strcpy(path, argv[1]);

  19.   show_txt(path);

  20.   free(path);

  21.   return 0;
  22. }

  23. int show_txt(char *path)
  24. {
  25.   struct stat statbuf;
  26.   struct dirent *dirp;
  27.   DIR *dp;
  28.   char *ptr;

  29.   int len = strlen(path);

  30.   if (lstat(path, &statbuf) < 0) {  /* lstat error */
  31.     perror("lstat error");
  32.     exit(1);
  33.   }

  34.   if (S_ISDIR(statbuf.st_mode) == 0) {  /* not a directory */
  35.       if (strcmp(&path[len-4], ".txt") == 0) {
  36.       printf("%s\n", path);
  37.     }
  38.     return 0;
  39.   }

  40.   ptr = path + len;
  41.   *ptr++ = '/';
  42.   *ptr = 0;

  43.   if ((dp = opendir(path)) == NULL) { /* open directory error */
  44.     perror("opendir error");
  45.     exit(1);
  46.   }

  47.   /* iterate the directory(dp)'s each element*/
  48.   while ((dirp = readdir(dp)) != NULL) {  
  49.     /* ignore the present directory and the last directory */
  50.     if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
  51.       continue;

  52.     strcpy(ptr, dirp->d_name);  /* append the element's name to path */
  53.     show_txt(path); /* call the show_txt(char*) recursively */
  54.   }

  55.   ptr[-1] = 0;
  56.   closedir(dp);
  57.   return 0;
  58. }
复制代码
回复

使用道具 举报

0

主题

0

好友

101

积分

注册会员

Rank: 2

8#
发表于 2013-12-20 17:40:16 |只看该作者

回帖奖励 +5

本帖最后由 shallecker 于 2013-12-20 17:45 编辑

带路径+txt
  1. # coding:utf-8
  2. import os, re

  3. for i in os.walk('d:'+os.sep):
  4.     for txt in i[2]:
  5.         try:
  6.             txt = re.match(r'(.*)(\.txt)', txt).group(0)
  7.             print os.path.join(i[0], txt)
  8.         except:
  9.             pass
复制代码
回复

使用道具 举报

0

主题

0

好友

65

积分

注册会员

Rank: 2

7#
发表于 2013-12-19 23:28:49 |只看该作者

回帖奖励 +5

本帖最后由 liucy 于 2013-12-19 23:29 编辑
  1. # coding:utf-8
  2. import os
  3. import fnmatch

  4. def findfile(inputdir):
  5.     txtlist = []
  6.     for parent, dirnames, filename in os.walk(inputdir):
  7.         for filenames in filename:
  8.             txtlist.append(filenames)
  9.         return fnmatch.filter(txtlist, '*.txt')

  10. if __name__ == "__main__":
  11.     inputdir1 = input("Please input your dir(like this: '/Users/lemon') : ")
  12.     print findfile(inputdir1)
复制代码
回复

使用道具 举报

0

主题

0

好友

17

积分

新手上路

Rank: 1

6#
发表于 2013-12-19 21:42:41 |只看该作者
fl0w 发表于 2013-12-19 21:39
这样不能找出嵌套的子文件夹中的以.txt结尾的文件

哈哈。谢谢提醒。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-18 17:22 , Processed in 0.026538 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部