Python小程序
swofford
posted @ 2010年10月18日 11:51
in Python
, 7796 阅读
前段时间想学TeX的,记得以前下过一个TeXbook的压缩包,不过文件名忘了,只记得里面有.pdf和.tex的文件,就想用Python写个小程序找到这文件。
逻辑很简单,遍历磁盘文件,若后缀名为.zip(rar的没有处理,因为Python没直接处理它的库,后来网上搜了下,还是有处理rar的库的),则打开该文件,若压缩包内有文件后缀名为.tex,则打印出路径和文件名。
# -*- coding: utf-8 -*- import os, zipfile for root, dirs, files in os.walk('C:\\'): for x in (root, dirs, files)[2]: if x.endswith('.zip'): path = root+'\\'+x zf = zipfile.ZipFile(path, 'r') files = zf.namelist() for f in files: if f.endswith('.tex'): print f + ' in ' + path
这次碰到的问题主要有这么几个:
1 os.walk生成的数据的结构
os.walk()用于遍历一个目录中的根目录,目录,和文件。(root, dirs, files)[0]是根目录,(root, dirs, files)[1]是根目录下的子目录,(root, dirs, files)[2]是根目录下的所有文件列表,依次遍历,可以自己动手实验一下。
2 后缀的处理
可以用.endswith()或者也可以用in
3 文件路径的生成
因为要读取文件,所以得生成路径,一开始root+x,出错,百查不得其解,后来上csdn问才发现该加个'\\',更好的办法是用os.path.join(),粗心啊粗心……
4 zipfile模块的使用
看文档
先写个简单版v0.1,以后慢慢完善。
2010年12月11日 01:38
《可爱的Python》上面给的也是这个解决方案。
不过我还是更喜欢用os.system调用Shell find命令这种方式。
2010年12月13日 08:56
@Lox: 1 walk()确实是从《可爱的Python》上学的
2 之前看过你的文章,os.system调用shell find应该是Linux上的吧,Windows上有类似解决方案么?
3 《可爱的python》上的中文编码问题,在Windows上快把我搞疯了,说ubuntu上都是 utf8 编码,应该没中文的问题吧?
2010年12月13日 10:43
@swofford: 1、《可爱的Python》做为饭后甜点确实很不错呢。
2、find是Linux上的工具。windows上的Dos命令可以调用,但是我相信应该没有find这么强大
3、ubuntu和各种linux都可以设定locale,指定系统的编码为UTF-8或者GBK,还有相关的时间日期格式,货币格式等等。但是中文也很烦的。
有个文件转码转码工具叫iconv,结合linux上的file命令可以实现初步的字符编码转换功能。不过这个也是比较烦的。
反正中文就是一个悲剧。还是期待有生之年能够看到Unicode一统江湖吧。
2010年12月13日 10:44
Python 3.0好像统一采用UTF-8了。不再采用ASCII。印象中是这样。
2010年12月13日 11:06
@Lox: 1 windows 上的dos怎么调用法……我都是用 everything,当然对付复杂条件的搜索肯定不如find了
2 曾经也看过有人说 py3.0 的字符统一了
3 《可爱的Python》上的那个编码的问题就一直卡在那动不了,后面的都没看……
2010年12月14日 02:25
@swofford: everything是什么东东?dos下面也有find命令。os.system()如果在linux下,调用的就是linux shell命令。如果在windows下,调用的就是dos命令了。
2010年12月16日 08:46
@Lox: everything 是 win 下搜索文件用的,速度爆快,还有 ftp 的功能,但我没用过,不过它只能搜索 ntfs 分区的文件,而且不能搜索文件内的内容
2024年8月05日 18:57
Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work.
2024年8月05日 19:13
Just lately, I didnrrrt offer lots of consideration for you to departing answers on-page page accounts and still have positioned replies actually much less. Examining by using your enjoyable article, can assist myself to do this sometimes.
2024年8月05日 19:14
Great post. I was checking continuously this blog and I am impressed! Extremely helpful information particularly the last part I care for such info a lot. I was seeking this certain info for a very long time. Thank you and good luck.
2024年8月05日 19:26
Gain typically major outstanding things -- you should know dozens of dishes inside:
2024年8月05日 19:29
The BioLean weight loss capsule stimulates the production of fat-burning hormones in the body. By triggering thermogenesis, it raises your body temperature, leading to the burning of additional calories from fat.
2024年8月05日 19:37
Thank you for the auspicious writeup. It in reality used to be a leisure account it. Look advanced to far delivered agreeable from you! However, how can we keep up a correspondence?
2024年8月05日 19:39
By detoxifying and enhancing liver function, Renew offers an effective solution for weight management and fostering a healthier lifestyle.
2024年8月05日 19:59
From 2020 year online mostbet Casino provides an opportunity to its customers near up to a hundred one-armed bandits of their own design.
2024年8月05日 20:14
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
2024年8月05日 20:15
Everything is very open with a precise explanation of the challenges.It was really informative. Your website is extremely helpful.Thanks for sharing!Look at my web page.
2024年8月05日 20:22
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you weren’t too busy looking for attention
2024年8月05日 20:25
you loved this information and you would like to receive additional facts relating to signs you are struggling with cocaine addiction kindly check out our own web
2024年8月05日 20:37
This is actually the kind of information I have been trying to find. Thank you for writing this information
2024年8月05日 20:47
you loved this information and you would like to receive additional facts relating to signs you are struggling with cocaine addiction kindly check out our own web
2024年8月05日 21:25
Wow, fantastic blog layout! How long have you been blogging for?
2024年8月05日 21:27
you beloved this post and you would like to receive a lot more details regarding mamba dating kindly check out our own
2024年8月05日 21:35
Link exchange is nothing else except it is only placing the other person’s weblog link on your page at
2024年8月05日 22:04
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me .. Thanks for all your help and wishing you all the success in your business
2024年8月05日 22:05
Link exchange is nothing else except it is only placing the other person’s weblog link on your page at
2024年8月05日 22:06
Link exchange is nothing else except it is only placing the other person’s weblog link on your page at
2024年8月05日 22:08
what you wish for to say regarding this article, in my view its in fact remarkable for me.
2024年8月05日 22:09
wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated
2024年8月05日 22:10
am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work
2024年8月05日 22:11
This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this.
2024年8月05日 22:12
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.