#coding=utf-8
import os,sys
reload(sys)
sys.setdefaultencoding("utf-8")
def scan_files_sub_dir(directory,prefix=None,postfix=None):
files_list=[]
for root, sub_dirs, files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
files_list.append(os.path.join(root,special_file))
elif prefix:
if special_file.startswith(prefix):
files_list.append(os.path.join(root,special_file))
else:
files_list.append(os.path.join(root,special_file))
return files_list
files = scan_files_sub_dir(".",None,".txt")
print files def scan_files(directory,prefix=None,postfix=None):
files_list=[]
for file in os.listdir(directory):
if postfix:
if file.endswith(postfix):
files_list.append(file)
elif prefix:
if file.startswith(prefix):
files_list.append(file)
else:
files_list.append(file)
return files_list
files = scan_files(".",None,".txt")
print files

另一种方法:

import os
def get_file_lists(init_dir,postfix=None):
file_lists = []
if not os.path.exists(init_dir):
print('%s not exist'%init_dir)
return file_lists
if not os.path.isdir(init_dir):
print('%s not dir'%init_dir)
return file_lists
for file in os.listdir(init_dir):
path = "%s\%s"%(init_dir,file)
if postfix and not file.endswith(postfix) and os.path.isfile(path):
continue
if not os.path.isdir(path):
file_lists.append(path)
if os.path.isdir(path):
file_lists.extend(get_file_lists(path,postfix))
return file_lists file_lists = get_file_lists(r'D:\Program Files\Java','java') file_writer = open('list.txt','w',encoding='utf-8')
for file in file_lists:
file_writer.write("%s\n"%file)
file_writer.close()

Python 扫面文件夹中的文件的更多相关文章

  1. 基于Python——实现两个文件夹中的文件拷贝

    [背景]当复制一个文件夹中的某文件到另一个文件夹中时是一件很容易的事情,可是如果存在很多文件夹中的文件需要一一拷贝,就会变的很繁琐,稍有不慎就会遗漏,今天就用Python来解决这个问题—— [代码实现 ...

  2. python遍历文件夹中所有文件夹和文件,os.walk

    python中可以用os.walk来遍历某个文件夹中所有文件夹和文件. 例1: import os filePath = 'C:/Users/admin/Desktop/img' for dirpat ...

  3. Python列出文件夹中的文件

    几乎所有的关于操作系统的内容可以在python 官方文档中找到:https://docs.python.org/3/library/os.html#module-os 其中os.path被单独列出:h ...

  4. python 遍历文件夹中所有文件

    '''使用walk方法递归遍历目录文件,walk方法会返回一个三元组,分别是root.dirs和files. 其中root是当前正在遍历的目录路径:dirs是一个列表,包含当前正在遍历的目录下所有的子 ...

  5. python将test01文件夹中的文件剪切到test02文件夹中

    将test01文件夹中的文件剪切到test02文件夹中 import shutil import os def remove_file(old_path, new_path): print(old_p ...

  6. cocos项目导入其它源文件时加入依赖库时,头文件提示找不到文件夹中的文件

    cocos项目导入其它源文件时加入依赖库时,头文件提示找不到文件夹中的文件解决方法: 选择项目属性->c/c++->常规,在附加包括项目中加上对应的文件夹 cocos test项目的库(所 ...

  7. 【转载】C#代码开发过程中如何快速比较两个文件夹中的文件的异同

    在日常的使用电脑的过程中,有时候我们需要比较两个文件夹,查找出两个文件夹中不同的文件以及文件中不同的内容信息,进行内容的校对以及合并等操作.其实使用Beyond Compare软件即可轻松比较,Bey ...

  8. java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数

    File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...

  9. C++获取文件夹中所有文件

    获取文件夹中的文件,用到过很多次,每次用的时候都要去查下,很烦,所以想自己写下,当然,借鉴了很多其他大佬的博客 主要实现的函数,如下: void getFiles( string path, vect ...

  10. IO流的练习2 —— 复制单级文件夹中的文件

    需求:把C:\Users\Administrator\Desktop\记录\测试里面的所有文件复制到 C:\Users\Administrator\Desktop\新建文件夹\copy文件夹中 分析: ...

随机推荐

  1. js:语言精髓笔记1--标识符与基本类型

    标识符: 命名: 语法以及类型----语法关键字                                           //逻辑 值(的存储位置)----变量和常量           ...

  2. POJ3659 Cell Phone Network(树上最小支配集:树型DP)

    题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...

  3. HDU2457 DNA repair(AC自动机+DP)

    题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...

  4. Revit二次开发示例:DesignOptions

    本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...

  5. [Unity3D]脚本中Start()和Awake()的区别

    Unity3D初学者经常把Awake和Start混淆. 简单说明一下,Awake在MonoBehavior创建后就立刻调用,Start将在MonoBehavior创建后在该帧Update之前,在该Mo ...

  6. Vijos 1180 (树形DP+背包)

    题目链接: https://vijos.org/p/1180 题目大意:选课.只有根课选了才能选子课,给定选课数m, 问最大学分多少. 解题思路: 树形背包.cost=1. 且有个虚根0,取这个虚根也 ...

  7. 新旧各版本的MySQL可以从这里下载

    http://downloads.mysql.com/archives/

  8. 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...

  9. elasticsearch1.0 升级2.2的数据备份和恢复

    近期由于elasticsearch的版本升级,需要研究下elasticsearch的快照(snapshot)和恢复(restore)功能.   先说下背景,目前环境采用的是elasticsearch1 ...

  10. yoman安装和使用

    http://yeoman.io/     安装到全局 sudo npm install -g yo 判断是否安装成 yo --version 常用命令 yo doctore yo --help 安装 ...