请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

【每日一坑 4】 查找文件

[复制链接]

0

主题

0

好友

4

积分

新手上路

Rank: 1

发表于 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

好友

49

积分

新手上路

Rank: 1

发表于 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

好友

26

积分

新手上路

Rank: 1

发表于 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
复制代码
回复

使用道具 举报

1

主题

0

好友

50

积分

注册会员

Rank: 2

发表于 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

好友

34

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

0

好友

4

积分

新手上路

Rank: 1

发表于 2015-11-8 00:55:33 |显示全部楼层
本帖最后由 小C 于 2015-11-8 00:56 编辑

#Filename: ex56.py

import os
import re

dirs = os.walk('.')
while True:
    try:
        tmp = dirs.next()
        for item in tmp[2]:#元组第三个为当前目录文件名
            if re.findall(r'\w*.txt', item) :
                print item
    except:
        StopIteration
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

发表于 2016-1-16 11:58:02 |显示全部楼层
本帖最后由 manhong2112 于 2016-1-16 14:38 编辑
  1. import re
  2. import os

  3. def check(dir):
  4.   for i in os.listdir(dir):
  5.     if os.path.isdir(i): check(dir + "/" + i)
  6.     print(str(i) + "\n" if re.search(r'.*\.txt',i) else "",end="")

  7. check(os.getcwd())
复制代码
回复

使用道具 举报

0

主题

1

好友

61

积分

注册会员

Rank: 2

发表于 2016-2-3 09:41:46 |显示全部楼层
敬爱的crossin先生,能不能给我们讲讲正则表达式,我看了几次,未遂,不敢接受自己的笨拙,帮我们理一理好吗,那么多记也记不得,写也无从下手,看别人的更加眼睛发晕。但是,我不能就这么放弃了。
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2016-2-3 10:51:20 |显示全部楼层
xqqxjnt1988 发表于 2016-2-3 09:41
敬爱的crossin先生,能不能给我们讲讲正则表达式,我看了几次,未遂,不敢接受自己的笨拙,帮我们理一理好 ...

55~59课是关于正则的,你在论坛里搜一下
http://crossin.me/search.php?mod ... =%E6%AD%A3%E5%88%99
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

1

好友

61

积分

注册会员

Rank: 2

发表于 2016-2-3 11:24:09 |显示全部楼层
交作业啦
献丑了
#!/usr/bin/python
#coding=utf-8
#author=xuqq
import os
#本程序试图找出指定文件夹中的所有以txt结尾的文件,包括所有嵌套的子文件夹。

def txt_search(path2):
    print "新加的目录是不是目录?os.path.isdir:\t",os.path.isdir(path2)
    print"新加的目录是不是绝对路径?os.path.isabs:\t",os.path.isabs(path2)
    print"新加的目录是否存在?os.path.exists:\t",os.path.exists(path2)
    print "*"*70
    time_flag=0
    txt_file_lists=[]
    if os.path.isdir(path2) and os.path.isabs(path2) and os.path.exists(path2):#os.path.isdir,  os.path.isabs,  os.path.exists
        for root,dir,files in os.walk(path2):               #os.walk(path)返回路径,目录名,该路径下的文件名,可以对os.walk用for循环,这样就可以遍历整个文件夹递归访问各个目录和文件
            time_flag= time_flag+1
            print "这是第%d次的深度递归:\t" %time_flag
            print "root:\t", root
            print "dir:\t", dir
            print "files:\t", files
            print "下面开始在这一层找txt:\t"
            print "$"*80
            for file_ob in files:
                suffix_tmp = os.path.splitext(file_ob)          #os.path.split(文件名)返回文件名和后缀名,是一个两个元素的列表,可以用【0】,【1】来分别访问
                print "文件的后缀名依次为:\t", suffix_tmp[1]
                if suffix_tmp[1]==".txt":
                    txt_file_lists.append(file_ob)
            print "*"*70
        print "最终找出来的txt文件如下所示:\t",txt_file_lists
    else:
        print "目录错误!"

def main():
    path1 = raw_input("pls input the path you want to search:\t")
    txt_search(path1)
   
if __name__=='__main__':
    main()
回复

使用道具 举报

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

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

GMT+8, 2024-3-29 14:17 , Processed in 0.025963 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部