1.如何比较两个文件的创建/修改时间?
确定搜索关键字:python compare file mtime/ctime
http://search.aol.com/aol/search?q=python+compare+file+ctime
file_list = sorted(file_list, key=os.path.getmtime) #参数中的file_list为要比较的文件列表
- http://stackoverflow.com/questions/14779775/how-to-compare-the-modified-date-of-two-files-in-python
- http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python
- http://stackoverflow.com/questions/7430928/python-comparing-date-check-for-old-file
file_mtime = os.path.getmtime(file_abs_name) #获取文件的mtime的方法
2.如何按照某个特定的顺序读取文件内容进行合并?
确定搜索关键字:python read/concatenate multiple files in order
- http://search.aol.com/aol/search?q=python+read+multiple+files+in+sequence
- http://search.aol.com/aol/search?q=python+read+multiple+files+in+order
- http://search.aol.com/aol/search?q=python+concatenate+files+in+order
- http://stackoverflow.com/questions/17749058/combine-multiple-text-files-into-one-text-file-using-python
- http://stackoverflow.com/questions/13613336/python-concatenate-text-files
- http://askubuntu.com/questions/352198/reading-all-files-from-a-directory
- http://stackoverflow.com/questions/10280963/python-read-from-multiple-files
- http://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python
- http://stackoverflow.com/questions/2508610/most-efficient-way-to-concatenate-and-rearrange-files #得具体情况具体分析
- http://stackoverflow.com/questions/9351553/most-efficient-way-to-concatenate-string #利用 str.join() 函数
- http://pythonforbiologists.com/index.php/introduction-to-python-for-biologists/reading-and-writing-files/
3.如何删除Python的字典中的某个元素?
http://search.aol.com/aol/search?q=python+delete+dict+items
总而言之就2种方式:del语句 或 重新赋值覆盖掉
- https://docs.python.org/2/tutorial/datastructures.html#dictionaries
- http://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary
4.如何删除Python的list中的某个元素?
- http://search.aol.com/aol/search?q=python+delete+list+items
- http://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value-in-python
- http://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index-in-python
- https://docs.python.org/2/tutorial/datastructures.html#the-del-statement #Python的del语句
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
https://docs.python.org/2/tutorial/datastructures.html#more-on-lists #Python的list操作集合
>>> a = [-1, 1, 66.25, 333, 333, 1234.5] >>> del a[0] >>> a [1, 66.25, 333, 333, 1234.5] >>> del a[2:4] >>> a [1, 66.25, 1234.5] >>> del a[:] >>> a []
5.如何用Python移动文件?
http://search.aol.com/aol/search?q=python+move+file+to+another+dir
推荐使用:shutil.move()
http://stackoverflow.com/questions/8858008/moving-a-file-in-python
os.rename() or shutil.move()
Although os.rename() and shutil.move() will both rename files, the command that is closest to the Unix mv command is shutil.move(). The difference is that os.rename() doesn’t work if the source and destination are on different disks, while shutil.move() doesn’t care what disk the files are on.
6.Python中的sleep方法该怎么写?
http://search.aol.com/aol/search?q=python+sleep
http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python
import time time.sleep(5) # delays for 5 seconds
Here is another example where something is run once a minute:
import time while True: print "This prints once a minute." time.sleep(60) # Delay for 1 minute (60 seconds)
7.Python中的字符串替换(长字符串替换的效率如何还不清楚?)
- http://search.aol.com/aol/search?q=Python+str.replace+efficient
- http://stackoverflow.com/questions/2484156/is-str-replace-replace-ad-nauseam-a-standard-idiom-in-python
- http://stackoverflow.com/questions/1919096/mass-string-replace-in-python
- http://stackoverflow.com/questions/5668947/python-string-replace-vs-re-sub
As long as you can make do with str.replace(), you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.
总而言之,建议就是:可以用字符串原生函数的地方就不要用正则表达式去完成类似的工作,效率隔了好多呢!
8.Python中的set和list的对比
http://stackoverflow.com/questions/2831212/python-sets-vs-lists
It depends on what you are intending to do with it. Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s), but are slower than lists when it comes to iterating over their contents. You can use the timeit module to see which is faster for your situation.
即,效率和具体的应用有关:set结构在用于判断某个元素是否存在的时候是非常快的;但在迭代枚举包含的元素时,要比list慢。
9.Python的join方法的使用
如何连接对象数组?
解法一:列表推导式(或生成器表达式)
', '.join([str(x) for x in list]) # list comprehension ', '.join(str(x) for x in list) # generator expression
解法二:善用map方法
''.join(map(str,list))
https://docs.python.org/2/library/stdtypes.html#str.join
10.Python的os.walk()方法返回内容的排序
之前一直使用Python的os.walk()方法遍历目录,也没什么其它的要求,用起来也是很爽的,但是近来有个需求:对os.walk()返回的列表按文件名/创建时间进行排序,就没法使用os.walk()自己返回的内容列表了,因为你不清楚它到底是怎么排序的,今天特地去搜了一下,原来如此:
http://search.aol.com/aol/search?q=python+os.walk%28%29+order
http://stackoverflow.com/questions/18282370/python-os-walk-what-order
os.walk uses os.listdir. Here is a the dostring for os.listdir: listdir(path) -> list_of_strings Return a list containing the names of the entries in the directory. path: path of directory to list The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.(是按任意顺序返回的,而且在不同的filesystem上返回结果不同……只有自己弄了。)
所以,如果你有自己的需要,可以使用sort或sorted()方法自定义进行次排序。
- https://docs.python.org/2/library/os.html#os.walk
- http://stackoverflow.com/questions/5667514/python-listing-dirs-in-a-different-order-based-upon-platform #排序结果和存储的文件系统有关
- https://docs.python.org/2/library/string.html#string.index
- http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python
- http://stackoverflow.com/questions/323689/python-list-slice-syntax-used-for-no-obvious-reason
借用str.index()或str.rindex()方法 && 列表切片可以达到某个目的:
files_dict = sorted( {item[item.rindex('_')+1:-4]:item for item in os.listdir(path)} )
''' In [5]: xx = '192.168.100.110-80_192.168.100.93-51664_3469669002.txt' In [6]: xx.index('_') Out[6]: 18 In [7]: xx[xx.index('_'):-4] Out[7]: '_192.168.100.93-51664_3469669002' In [8]: xx[xx.rindex('_'):-4] Out[8]: '_3469669002' In [9]: xx[xx.rindex('_')+1:-4] Out[9]: '3469669002' In [10]: xx.rindex('_') Out[10]: 39 In [11]: xx.index?? Type: builtin_function_or_method String form: <built-in method index of str object at 0x036DD9D0> Docstring: S.index(sub [,start [,end]]) -> int Like S.find() but raise ValueError when the substring is not found. In [12]: xx.rindex?? Type: builtin_function_or_method String form: <built-in method rindex of str object at 0x036DD9D0> Docstring: S.rindex(sub [,start [,end]]) -> int Like S.rfind() but raise ValueError when the substring is not found. '''