易截截图软件、单文件、免安装、纯绿色、仅160KB

Python中的OS模块

os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,如posix或NT系统平台,os模块会根据不同的平台进行相应的操作.本节内容将对os模块提供的函数进行详细的解读.
1.1 文件操作函数
1.1.1 open()函数提供创建、打开、修改文件的功能。
Example 1-1. Using the os Module to Rename and Remove Files
#Filename: os-example-1.py
import os
import string
def replace(file, search_for, replace_with):
    # replace strings in a text file
    back = os.path.splitext(file)[0] + ".bak"
    temp = os.path.splitext(file)[0] + ".tmp"
    try:
        # remove old temp file, if any
        os.remove(temp)
    except os.error:
        pass
    fi = open(file)
    fo = open(temp, "w")
    for s in fi.readlines():
        fo.write(string.replace(s, search_for, replace_with))
    fi.close()
    fo.close()
    try:
        # remove old backup file, if any
        os.remove(back)
    except os.error:
        pass
    # rename original to backup...
    os.rename(file, back)
    # ...and temporary to original
    os.rename(temp, file)
# try it out!
file = "samples/sample.txt"
replace(file, "hello", "tjena")
replace(file, "tjena", "hello")
1.1.2 rename()和remove()函数,对文件进行重命名和删除操作.请参照例1-1
1.2 目录操作
1.2.1 listdir()函数,返回指定目录下所有文件名,并保存于一列表中.但当前目录标记(.)和父目录标记(..)不在其中.
Example 1-2. Using the os Module to List the Files in a Directory
File: os-example-2.py
import os
for file in os.listdir("samples"):
 


相关文档:

用Python直接写UTF 8文本文件

当我们这样建立文件时
f =
file('x1.txt', 'w')
f.write(u'中文')
f.colse()

接结果应该是类似
    f.write(u'中文')
UnicodeEncodeError: 'ascii'
codec can't encode characters in position 0-16: ordinal not in
range(128)
要直接写 utf-8 文件怎么办呢?
import codecs
f = codecs. ......

python中%符号详解

%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C
年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F
年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名 ......

Python word


超群.com的博客
Python转换office word文件为HTML
这里测试的环境是:windows xp,office 2007,python 2.5.2,pywin32 build
213,原理是利用win32com接口直接调用office
API,好处是简单、兼容性好,只要office能处理的,python都可以处理,处理出来的结果和office word里面“另存为”一致。
#!/usr/bin/en ......

使用C语言扩展Python(一)

开发环境:Ubuntu9.10,python2.6,gcc4.4.11,ubuntu下的python运行包和开发包是分开的,因此需要在新利得里面安装python-all-dev,从而可以在代码中引用python的头文件和库。2.下面是一个最简单的可以供python调用的c扩展模块,假设c程序文件名为foo.c:代码#include <Python.h>
static PyObject* foo_b ......

Python为类定义“拷贝构造函数”

初学Python,这么做好像有点2,凑合能用:
class MyClass():
def __init__(self, n = 10):
self._Field = n
def __getitem__(self, range):
return MyClass(self._Field)
obj1 = MyClass()
obj2 = obj1
obj3 = obj1[:]
obj1._Field = 100
obj4 = MyClass(123)
print obj1._Field, obj2. ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号