os.popen函数
import os
def get_doc(module):
a = 'pydoc %s' % module
m = os.popen(a).read()
return m
raw_input 函数
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str = raw_input("请输入:");
print "你输入的内容是: ", str
input函数
input 可以接收一个Python表达式作为输入,并将运算结果返回。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str = input("请输入:");
print "你输入的内容是: ", str
文件操作
r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+ 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+ 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
# 写入一个文件
fo = open("foo.txt", "wb")
fo.write( "www.runoob.com!\nVery good site!\n");
# 关闭打开的文件
fo.close()
或者 with open("foo.txt", "wb") as fo: #这个不需要close
fo.write( "www.runoob.com!\nVery good site!\n");
# 读取一个文件 open
fo = open("foo.txt", "r+")
str = fo.read(10); #读取10个字符
str = fo.readline(); #读取一行
print "读取的字符串是 : ", str
# 查找当前位置
position = fo.tell();
print "当前文件位置 : ", position
# 把指针再次重新定位到文件开头
position = fo.seek(0, 0);
str = fo.read(10);
print "重新读取字符串 : ", str
# 关闭打开的文件
fo.close()
#重新命名文件
import os
# 重命名文件test1.txt到test2.txt。
os.rename( "test1.txt", "test2.txt" )
# 删除一个已经存在的文件test2.txt
os.remove("test2.txt")
linecache使用:
print linecache.getline(”temp.txt“,4) 获取第四行的内容
lines = linecahe.getlines(”temp.txt“)
print lines 得到一个list list的每个元素是每行内容
目录
# 创建目录test
os.mkdir("test")
# 将当前目录改为"/home/newdir"
os.chdir("/home/newdir")
# 给出当前的目录
print os.getcwd()
# 删除”/tmp/test”目录
os.rmdir( "/tmp/test" )
文件夹操作
import os
os.path.isdir(参数) #判断是否是文件夹
查看一个当前文件的权限
oct(os.stat(‘e.py’).st_mode) #oct 转成八进制 st_mode 十进制的权限
增加可执行权限
os.chmod(‘e.py’,os.stat(‘e.py’).st_mode | stat.S_IXUSR)
如何读写文本文件
文本文件编码格式已知,在python2.x python3.x 中分别如何读取该文件
解决方案:
字符串的语义发生了变化:
python 2 python3
str bytes
unicode str
python 2.x 写入文件前对unicode编码,读入文件后对二进制字符串编码
python 3.x open函数指定't'的文本模式,endcoding指定编码格式
python 2.x
unicode 和 str 之间的转换:
s=u'你好'
print s.encode('utf8') #将得到一个str的字符串
print s.encode('gbk')
s2 = s.encode('utf8')
print s2.decode('utf8') #解码操作
f = open('py2.txt','w') #以只写的方式打开
s= '你好'
f.write(s.encode('gbk'))
f.close()
f = open('py2.txt','r')
t = f.read()
print t.decode('gbk')
python 3.x
bytes 字符串的表示 b'efjwefwief'
unicode 字符串的表示 '你好' #和python 2.x 不同 python 2.x 需要加 u'你好' 表示unicode字符串
open ('py3.txt','wt') #以t文本模式进行写操作
f = open('py3.txt','wt',endoding='utf8')
f.write('你好,测试')
f.close()
f = open('py3.txt','rt',endoding='utf8')
s = f.read()
print(s)
如何处理二进制文件
使用python 分析一个wav文件头部的信息,处理音频数据
f = open('demo.wav','rb') #以二进制的模式进行打开文件
合并一个文件夹下所有文件的内容
import os
#使用递归去解决
def merge(folder_path):##习题2
if not os.path.exists(folder_path):
return 'not exists'
for f in os.listdir(folder_path):
file_path = os.path.join(folder_path,f)
if os.path.isdir(file_path):
merge(file_path)
else:
merge_file = open('/tmp/merge_test','ab+')
content = open(file_path,'r').read()
merge_file.write(content)
merge_file.close()
merge('/tmp/5')
列出文件夹下面的所有文件
import os
rootdir = u'E:\python'
list = os.listdir(rootdir)
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isfile(path) and path.find("~") == -1:
print path