Python 扫面文件夹中的文件
#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 扫面文件夹中的文件的更多相关文章
- 基于Python——实现两个文件夹中的文件拷贝
[背景]当复制一个文件夹中的某文件到另一个文件夹中时是一件很容易的事情,可是如果存在很多文件夹中的文件需要一一拷贝,就会变的很繁琐,稍有不慎就会遗漏,今天就用Python来解决这个问题—— [代码实现 ...
- python遍历文件夹中所有文件夹和文件,os.walk
python中可以用os.walk来遍历某个文件夹中所有文件夹和文件. 例1: import os filePath = 'C:/Users/admin/Desktop/img' for dirpat ...
- Python列出文件夹中的文件
几乎所有的关于操作系统的内容可以在python 官方文档中找到:https://docs.python.org/3/library/os.html#module-os 其中os.path被单独列出:h ...
- python 遍历文件夹中所有文件
'''使用walk方法递归遍历目录文件,walk方法会返回一个三元组,分别是root.dirs和files. 其中root是当前正在遍历的目录路径:dirs是一个列表,包含当前正在遍历的目录下所有的子 ...
- python将test01文件夹中的文件剪切到test02文件夹中
将test01文件夹中的文件剪切到test02文件夹中 import shutil import os def remove_file(old_path, new_path): print(old_p ...
- cocos项目导入其它源文件时加入依赖库时,头文件提示找不到文件夹中的文件
cocos项目导入其它源文件时加入依赖库时,头文件提示找不到文件夹中的文件解决方法: 选择项目属性->c/c++->常规,在附加包括项目中加上对应的文件夹 cocos test项目的库(所 ...
- 【转载】C#代码开发过程中如何快速比较两个文件夹中的文件的异同
在日常的使用电脑的过程中,有时候我们需要比较两个文件夹,查找出两个文件夹中不同的文件以及文件中不同的内容信息,进行内容的校对以及合并等操作.其实使用Beyond Compare软件即可轻松比较,Bey ...
- java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数
File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...
- C++获取文件夹中所有文件
获取文件夹中的文件,用到过很多次,每次用的时候都要去查下,很烦,所以想自己写下,当然,借鉴了很多其他大佬的博客 主要实现的函数,如下: void getFiles( string path, vect ...
- IO流的练习2 —— 复制单级文件夹中的文件
需求:把C:\Users\Administrator\Desktop\记录\测试里面的所有文件复制到 C:\Users\Administrator\Desktop\新建文件夹\copy文件夹中 分析: ...
随机推荐
- struts2总结四:Action与Form表单的交互
struts2 Action获取表单数据的方式有三种:1.通过属性驱动的方式.2.模型驱动方式.3.使用多个model对象的属性. 1.通过属性驱动式 首先在jsp里面编写form表单的代码如下: & ...
- spring实战四之Bean的自动装配(注解方式)
使用注解装配: 从spring2.5开始,Spring启用了使用注解自动装配Bean的属性,使用注解方式自动装配与在XML中使用 autowire 属性自动装配并没有太大区别,但是使用注解方式允许更细 ...
- 转 BHO API HOOK Wininet基于IE编程的一些资料
BHO原理:推荐vc base中的文章: 如何使用BHO定制你的Internet Explorer浏览器 API HOOK的基本原理:推荐C++ builder 研究中的文章: API Hook基 ...
- 水题 HDOJ 4716 A Computer Graphics Problem
题目传送门 /* 水题:看见x是十的倍数就简单了 */ #include <cstdio> #include <iostream> #include <algorithm ...
- 什么是 IntentService
service 默认也运行在 UI 线程,所以里面不能直接做耗时操作,要做耗时操作还得开启子线程来做. IntentService 就是一个 Service, 只不过里面给你默认开启了一个子线程来处理 ...
- .net操作xml文件(新增.修改,删除,读取)---datagridview与xml文件
参考网址: http://www.cnblogs.com/liguanghui/archive/2011/11/10/2244199.html 很详细的,相信能给你一定的帮助.
- web app
*********Modernizr作为开发HTML5必要的js工具************ Modernizr作为开发HTML5必要的js工具 *********字体大小设 rem********* ...
- ajax利用json进行服务器与客户端的通信
1.JQuery中$.ajax()方法参数详解 http://blog.sina.com.cn/s/blog_4f925fc30100la36.html 2.服务器端获取String que=requ ...
- 字符编解码的故事(ASCII,ANSI,Unicode,Utf-8区别)
(关于字符编码的深入解释,请参见我的原创文章<关于字符编码,你所需要知道的>.) 此文为转载,有少许修订,原文出处不详. 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同 ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...