python 获取当前目录下文件(转)
今天继续整理原来写的 python 代码,下面是获取文件信息的 python 处理代码。
#!/usr/bin/env python2
#-*-encoding:utf-8-*-import os,sys
def listdir(dir,file):
file.write(dir +'\n')
fielnum =0
list = os.listdir(dir)#列出目录下的所有文件和目录
for line in list:
filepath = os.path.join(dir,line)
if os.path.isdir(filepath):#如果filepath是目录,则再列出该目录下的所有文件
myfile.write(' '+ line +'//'+'\n')
for li in os.listdir(filepath):
myfile.write(' '+li +'\n')
fielnum = fielnum +1
elif os.path:#如果filepath是文件,直接列出文件名
myfile.write(' '+line +'\n')
fielnum = fielnum +1
myfile.write('all the file num is '+ str(fielnum))
dir = raw_input('please input the path:')
myfile = open('list.txt','w')
listdir(dir,myfile)
myfile.close()
G:\codes\python\file_info
list.txt
list2.txt
list_filenames.py
list_filenames2.py
list_files+.py
test//
g.txt
mmm.pptx
test2//
list.txt
test3
all the file num is9
#!/usr/bin/env python2
#-*-encoding:utf-8-*-"""os.walk(path),遍历path,返回一个对象,
他的每个部分都是一个三元组,
('目录x',[目录x下的目录list],目录x下面的文件)"""import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name)+'\n')
for name in dirs:
print(os.path.join(name))
fileinfo.write(' '+ os.path.join(root,name)+'\n')
dir = raw_input('please input the path:')
dir = r'G:\codes\python\file_info'
fileinfo = open('list2.txt','w')
walk_dir(dir,fileinfo)
fileinfo.close()
>>>================================ RESTART ================================
>>>
please input the path:G:\codes\python\file_info
list.txt
list2.txt
list_filenames.py
list_filenames2.py
list_files+.py
test
test2
g.txt
mmm.pptx
list.txt
test3
234.txt
G:\codes\python\file_info\list.txt
G:\codes\python\file_info\list2.txt
G:\codes\python\file_info\list_filenames.py
G:\codes\python\file_info\list_filenames2.py
G:\codes\python\file_info\list_files+.py
G:\codes\python\file_info\test
G:\codes\python\file_info\test2
G:\codes\python\file_info\test\g.txt
G:\codes\python\file_info\test\mmm.pptx
G:\codes\python\file_info\test2\list.txt
G:\codes\python\file_info\test2\test3
G:\codes\python\file_info\test2\test3\234.txt
#!/usr/bin/env python2
#-*-encoding:utf-8-*-import os
import sys
import md5def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
path = os.path.join(root,name)
md5v = sumfile(path)
newpath = path.replace(dir,'')
fileinfo.write(newpath + ':' + md5v + '\n')def sumfile(fpath):
m = md5.new()
fobj = open(fpath)
while True:
d = fobj.read(8096)
if not d:
break
m.update(d)
return m.hexdigest()#获取脚本文件的当前路径
def cur_file_dir():
#获取脚本路径
path = sys.path[0]
#判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
#打印结果
print cur_file_dir()def main(): #dir = raw_input('please input the path:') dir = cur_file_dir() fileinfo = open('list3.txt','w') walk_dir(dir,fileinfo)if __name__ == '__main__': main()
\list.txt:0a732356981d24beab0d0c1c4092b2a7
\list2.txt:6cebc24b3a996be27c732557a2ce545f
\list3.txt:d41d8cd98f00b204e9800998ecf8427e
\list_filenames.py:9201c1eefcaf2fd04478cc2b6430c678
\list_filenames2.py:ca3f74bdb0a5485dd5d95d77da80141b
\list_files+.py:720d569d2f07f0c80e0d695db31749ab
\test\g.txt:d41d8cd98f00b204e9800998ecf8427e
\test\mmm.pptx:6f0e81ad84c22838337f0f619080e7f2
\test2\list.txt:d41d8cd98f00b204e9800998ecf8427e
\test2\test3\234.txt:d41d8cd98f00b204e9800998ecf8427e
python 获取当前目录下文件(转)的更多相关文章
- python 获取当前目录下的文件目录和文件名
python 获取当前目录下的文件目录和文件名 os模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os ...
- python获取目录下文件夹名称
path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir
- python 运行当前目录下的所有文件
查看当前目录下所有py文件(本身除外run) import os file_list = os.listdir(os.getcwd()) # 获取当前目录下所有的文件名print(file_list ...
- python生成器 获取 目录下文件
# os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...
- Python获取当前路径下的配置文件
Python获取当前路径下的配置文件 有的时候想读取当前目录下的一个配置文件.其采用的办法是: import os # 获取当前路径 curr_dir = os.path.dirname(os.pat ...
- File获取当前目录下的所有子项 listFiles()
package seday03; import java.io.File; /** * 获取一个目录中的所有子项 * @author xingsir */public class ListFilesD ...
- spring java 获取webapp下文件路径
spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- springboot打成jar后获取classpath下文件失败
原文链接:https://blog.csdn.net/qq_18748427/article/details/78606432 springboot打成jar后获取classpath下文件失败 使用如 ...
随机推荐
- LayaAir引擎——(五)
LayaAir引擎——关于地图详解 所需要的软件: LayaAirIDE1.0.2版本 在LayaAir引擎中与地图相关的类: 1.laya.map.TiledMap TiledMap类 地图以层 ...
- 用jQuery判断一个元素的各种状态
用jQuery判断一个元素是否显示 用jQuery判断一个元素是否显示:$(element).is(":visible"); 类似的,判断一个元素是不是第一个子元素:$(ele ...
- 14——小心copying行为
资源的copying行为决定对象的copying行为. 抑制copying行为,使用引用计数.
- 加密web.config
当我们要进行数据库的连接时,就会根据<%$ connectionsStrings:MyConnectionStringName %>这个表达式在Web.config文件中找到和MyConn ...
- BT协议分析(1)—1.0协议
简述 BT下载是采用P2P的下载方式,下载的大致形式采用如下图所示,处于图示中心的称为Tracker服务器,其余称为Peer. 缺点 1.资源的安全性 2.资源的实效性(没有上传者则BT也将失效) ...
- Codeforces Round #379 (Div. 2) A B C D 水 二分 模拟
A. Anton and Danik time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- hdu 2602 Bone Collector(01背包)模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- java内存分配--引用
栈内存 对象地址 堆内存 存放属性 public class TestDemo{ public static void main(String args[]){ Person perA =new ...
- iptables的扩展匹配
iptables的匹配条件 一.通用匹配:-s.-d.-p.-i.-o 二.扩展匹配 1.隐含扩展:使用-p{tcp|udp|icmp}指定某特定协议后,自动能够对协议进行扩展 -p tcp --dp ...