Python 中列出目录中所有文件的方法
Python代码
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
print dir + os.sep + f
print '----------- all dir'
for root, dirs, files in os.walk(dir):
for name in files:
print os.path.join(root, name)
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
print dir + os.sep + f
print '----------- all dir'
for root, dirs, files in os.walk(dir):
for name in files:
print os.path.join(root, name)
前面的 os.listdir 可以列出 dir 里面的所有文件和目录,但不包括子目录中的内容。os.walk 可以遍历下面的所有目录,包括子目录。
相关文档:
用Python导出QQ空间的日志到WordPress
文章来源:http://www.keakon.cn/bbs/thread-964-1-1.html方法很简单,找出日志的地址,再遍历列出日志的内容。
因为单纯导出没用,还得转换成其他格式,所以我保存到一个列表里,每篇日志都对应其中的一个字典元素,字典的属性都用unicode编码。
然后dump出来,可以方便以后用Pyth ......
下载了很多压缩文件,就写了一个脚本
在Python中使用winrar命令,所以一般压缩文件都支持
有些压缩文件Python中还没有相应的库
你得先把winrar添加到path环境变量中
把代码保存为rar.py
在dos下使用,如:rar.py "D:\A B\C" mkdir
#rar.py
#decompress with winrar
#arguments :filename directory opt
# op ......
网络时间服务器 一般都遵循 RFC868 协议标准.
按该标准 附下面 Python 源码.
# -*- coding: utf-8 -*-
import socket,sys,time
#时间服务器
host = "stdtime.gov.hk"
#端口
port = 37
#时区
curtz = 8
#连接服务器,并接收返回
try:
host = socket.gethostbyname(host)
s = socket.socket(socket.AF_INET,soc ......
type相关:
所有自定义类A
其实例,如 a = A()
使用type运算符返回的都是<type, 'instance'>
而基本类型
比如 b = 3
type(b),结果是<type, 'int'>
python的type不像C++中的typeid那样,可以显示类名。
(注:对于没有virtual函数的类而言,typeid是编译时期的事情(也就是静态类型);对于有virtual函 ......