with open('somefile.txt', 'rt', newline='') as f:
pass
如果知道文件的编码格式,可以以指定的编码读取
with open('somefile.txt', 'rt', encoding='latin-1') as f:
pass
打印输出至文件中
在 print() 函数中指定 file 关键字参数
def print_out():
with open('test.txt', 'wt') as f:
print("Hello World", file=f)
使用其他分隔符或行终止符打印
可以使用在 print() 函数中使用 sep 和 end 关键字参数
>>> print ('aaa', 90, 51, sep=',', end='!!!')
aaa,90,51!!!
#在输出中禁止换行
>>> for i in range(5):
... print (i, end=' ')
0 1 2 3 4
读写字节数据
使用模式为 rb 或 wb 的 open() 函数来读取或写入二进制数据
字节字符串和文本字符串在迭代时返回值是不同的
>>> t = "hello world"
>>> t[0]
'h'
>>> t = b"hello world"
>>> t[0]
104
从二进制模式的文件中读取或者写入数据,需要相应的解码和编码
def wr_encode():
with open('test.bin', 'wb') as f:
f.write('hello world'.encode('utf-8'))
def rd_decode():
with open('test.bin', 'rb') as f:
data = f.read(16)
text = data.decode('utf-8')
防止写的文件被覆盖
在 open() 函数中使用 x 模式来代替 w 模式来防止文件被重复写入
>>> with open('test.txt', 'wt') as f:
... f.write('Hello world')
>>> with open('test.txt', 'xt') as f:
... f.write('Hello world')
Traceback (most recent call last):
File "", line 1, in
with open('test.txt', 'xt') as f:
FileExistsError: [Errno 17] File exists: 'test.txt'
#也可以在写前面检测文件是否存在
>>> import os
>>> os.path.exists('test.txt')
True
from functools import partial
RECORD_SIZE = 32
def size_rb():
with open('somefile.data', 'rb') as f:
records = iter(partial(f.read, RECORD_SIZE), b'')
for r in records:
print (r)
读取二进制数据到可变缓冲区中
直接读取二进制数据到一个可变缓冲区中,而不需要做任何的中间复制操作
文件对象的 readinto() 方法能被用来为预先分配内存的数组填充数据
返回实际读取的字节数
import os
def read_into_buffer(filename):
#设定缓冲区大小
buf = bytearray(os.path.getsize(filename))
with open(filename, 'rb') as f:
f.readinto(buf)
return buf
if __name__ == '__main__':
with open('sample.bin', 'wb') as f:
f.write(b'Hello World')
#读取数据到缓冲区
buf = read_into_buffer('sample.bin')
with open('newsample.bin', 'wb') as f:
f.write(buf)
#结果会返回目录中所有文件列表
>>> os.listdir('.')
['ch5_10.py', 'ch5_2.py', 'ch5_4.py', 'ch5_8.py', 'ch5_9.py', 'data', 'newsample.bin', '
sample.bin', 'somefile.data', 'test.bin', 'test.gz', 'test.txt']
#执行某种过滤
>>> name = [name for name in os.listdir() if os.path.isfile(os.path.join('.', name))]
>>> name
['ch5_10.py', 'ch5_2.py', 'ch5_4.py', 'ch5_8.py', 'ch5_9.py', 'data', 'newsample.bin', '
sample.bin', 'somefile.data', 'test.bin', 'test.gz', 'test.txt']
>>> name = [name for name in os.listdir() if os.path.isdir(os.path.join('.', name))]
>>> name
['test']
>>> pyfils = [name for name in os.listdir() if name.endswith('.py')]
>>> pyfils
['ch5_10.py', 'ch5_2.py', 'ch5_4.py', 'ch5_8.py', 'ch5_9.py']
收集文件的其他数据
import os
import glob
def main():
pyfiles = glob.glob('*.py')
print (pyfiles)
name_sz_date = [(name, os.path.getsize(name), os.path.getmtime(name))\
for name in pyfiles]
for name, size, mtime in name_sz_date:
print (name, size, mtime)
if __name__ == '__main__':
main()
import urllib.request
import io
def encode_text():
u = urllib.request.urlopen('http://www.python.org')
f = io.TextIOWrapper(u, encoding='utf-8')
#编码成utf-8
text = f.read()
print (text)
if __name__ == '__main__':
encode_text()
创建临时文件和文件夹
在程序执行时创建一个临时文件或目录,并希望使用完之后可以自动销毁掉
w+t'为文本模式,delete=False,表示临时文件不会被删除
from tempfile import NamedTemporaryFile, TemporaryDirectory
def create_tmp():
#'w+t'为文本模式
with NamedTemporaryFile('w+t', delete=False) as f:
print ('filename is :', f.name)
f.write('Hello World\n')
f.write('Testing\n')
f.seek(0)
data = f.read()
with TemporaryDirectory() as dirname:
print ('dirname is :', dirname)
if __name__ == '__main__':
create_tmp()
可以自己定制自己的临时文件规则
>>> from tempfile import NamedTemporaryFile
>>> f = NamedTemporaryFile(prefix='mytemp', suffix='.txt', dir='/tmp')
>>> f.name
'/tmp/mytempurb_uyz4.txt'
>>>
Open Source Obfuscators
ProGuard
http://java-source.net/open-source/obfuscators/proguardProGuard is a free Java class file shrinker and obfuscator. It can detect and remove unused classes, fields, m
Maven简介
是什么?
Is a software project management and comprehension tool.项目管理工具
是基于POM概念(工程对象模型)
[设计重复、编码重复、文档重复、构建重复,maven最大化消除了构建的重复]
[与XP:简单、交流与反馈;测试驱动开发、十分钟构建、持续集成、富有信息的工作区]
功能:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
前面3步应该没有问题,主要的问题是执行make的时候,出现了异常。
异常一:
make[2]: cc: Command not found
异常原因:没有安装g
nimbus结点配置(storm.yaml)信息:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional inf