# 分数/有理数 加减乘除正常用 # 创建 小数\字符串\整数 都行 for v in [0.1, 0.5, 1.5, 2.0]: print('{} = {}'.format(v, fractions.Fraction(v))) for s in ['1/2', '2/4', '3/6']: f = fractions.Fraction(s) print('{} = {}'.format(s, f)) for s in ['0.5', '1.5', '2.0', '5e-1']: f = fractions.Fraction(s) print('{0:>4} = {1}'.format(s, f)) for n, d in [(1, 2), (2, 4), (3, 6)]: f = fractions.Fraction(n, d) print('{}/{} = {}'.format(n, d, f)) # 精确度 import fractions import math
print('{}:'.format(r'./')) print(' Size:', stat_info.st_size) print(' Permissions:', oct(stat_info.st_mode)) print(' Owner:', stat_info.st_uid) print(' Device:', stat_info.st_dev) print(' Created :', time.ctime(stat_info.st_ctime)) print(' Last modified:', time.ctime(stat_info.st_mtime)) print(' Last accessed:', time.ctime(stat_info.st_atime))
# touch()创建一个文件或者更新修改时间 # chmod()更改权限
./:
Size: 4096
Permissions: 0o40777
Owner: 0
Device: 3527538052
Created : Sat Oct 22 08:58:30 2022
Last modified: Thu Mar 16 13:20:36 2023
Last accessed: Thu Mar 16 13:36:45 2023
path : ..\_post
name : _post
suffix:
stem : _post
glob
1 2 3 4 5 6 7 8 9 10
import glob
# 查找匹配,类似正则
# 只会匹配目中的所有路径,不会递归的搜索到子目录 # *匹配多个 ?匹配一个 for name insorted(glob.glob('./*.md'))[:5]: print(name)
gettempdir(): C:\Users\Ada\AppData\Local\Temp
gettempprefix(): tmp
Some data
temp.name:
'd:\\BaiduSyncdisk\\Blog\\source\\_posts\\prefix_0iyyalif_suffix'
Exists after close: False
C:\Users\Ada\AppData\Local\Temp\tmpm9xfa5bw
This file is deleted.
Directory exists after? False
Contents after: []
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import tempfile # SpooledTemporaryFile 到一定阈值才写进去文件 with tempfile.SpooledTemporaryFile(max_size=1000, mode='w+t', encoding='utf-8') as temp: print('temp: {!r}'.format(temp.name))
for i inrange(3): temp.write('This line is repeated over and over.\n') print(temp._rolled, temp._file) print('rolling over') # 手动指定 temp.rollover() print(temp._rolled, temp._file)
temp: None
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
rolling over
True <tempfile._TemporaryFileWrapper object at 0x000001E88C4ECE20>
# print('\nArchive contents:') # with tarfile.open('example.tar.gz', 'r') as t: # for n in t.getnames(): # print(n)
1 2 3 4 5
# import shutil # # 解压缩文件 # for format, exts, description in shutil.get_unpack_formats('文件名'): # print('{:<5}: {}, names ending in {}'.format( # format, description, exts))
# 使用操作系统虚拟内存来访问数据 withopen('./doing.ipynb', 'r') as f: with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as m: # 指针向右移动10 print('First 10 bytes via read :', m.read(10)) # 分片操作符将指针移回起始位置,分片在向右移动10 print('First 10 bytes via slice:', m[:10]) # 再向右移动10 print('2nd 10 bytes via read :', m.read(10)) # m.flush() 修改 # 单独回滚 # m.seek(0) # rewind # f.seek(0) # rewind
First 10 bytes via read : b'{\n "cells"'
First 10 bytes via slice: b'{\n "cells"'
2nd 10 bytes via read : b': [\n {\n '
codes
1 2 3 4 5
# 编码! # unicode # utf-8 # assic # ...
io
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import io
output = io.StringIO() output.write('This goes into the buffer. ') # 可以替换成文件 print('And so does this.', file=output) # Retrieve the value written print(output.getvalue()) output.close() # discard buffer memory
# Initialize a read buffer input = io.StringIO('Inital value for read buffer') # Read from the buffer print(input.read())
This goes into the buffer. And so does this.
Inital value for read buffer
1 2 3 4 5 6 7 8 9 10
output = io.BytesIO() output.write('This goes into the buffer. '.encode('utf-8')) output.write('ÁÇÊ'.encode('utf-8')) # Retrieve the value written print(output.getvalue()) output.close() # discard buffer memory # Initialize a read buffer input = io.BytesIO(b'Inital value for read buffer') # Read from the buffer print(input.read())
b'This goes into the buffer. \xc3\x81\xc3\x87\xc3\x8a'
b'Inital value for read buffer'