python基础查漏补缺5--输入和输出
1. 字符转换格式
说 明 符 | 含 义 |
d | 整数 |
o | 八进制值 |
x | 小写十六进制数 |
X | 大写十六进制数 |
e | 小写科学计数法表示的浮点数 |
E | 大写科学计数法表示的浮点数 |
f | 浮点数 |
s | 字符串 |
% | %字符 |
>>> x = 0.01234567890123456789 >>> print('x = %f' % x) x = 0.012346 >>> print('x = %e' % x) x = 1.234568e-02 >>> print('x= %E' % x) x= 1.234568E-02 >>> print('x = %o' % 100) x = 144 >>> print('x = %x' % 100) x = 64 >>> print('x = %x' % 200) x = c8 >>> print('x = %X' % 200) x = C8 >>> print('x = %s' % 'str') x = str >>> print('x = %d' % 200) x = 200
>>> print('x = %.2f' % x)
x = 0.01
2. 格式字符串 format
>>> # 命名替换 >>> 'my {pet} has {prob}'.format(pet = 'dog', prob = 'fleas') 'my dog has fleas' >>> # 位置替换 >>> 'my {0} has {1}'.format('dog', 'fleas') 'my dog has fleas' >>> # 使用转换说明符 >>> '1/81 = {x}'.format(x = 1/81) '1/81 = 0.012345679012345678' >>> '1/81 = {x:f}'.format(x = 1/81) '1/81 = 0.012346' >>> '1/81 = {x:.2f}'.format(x = 1/81) '1/81 = 0.01' >>> # 使用大括号来指定格式设置参数 >>> 'num = {x:.{d}f}'.format(x = 1/81, d=4) 'num = 0.0123' >>> # 格式化字符串更复杂一些,适合复杂任务,比如创建网页或者格式化邮件
3. 文件夹处理
函 数 名 | 作用 |
os.getcwd() | get current work directory 返回当前工作目录的名称 |
os.listdir(p) | list directory返回一个字符串列表,其中包含路径 p 指定的文件夹中所有文件和文件夹的名称 |
os.chdir(p) | change directory将当前工作目录设置为路径 p |
os.path.isfile(p) | 当路径 p 指定的是一个文件的名称时,返回 True,否则返回 False |
os.path.isdir(p) | 当路径 p 指定的是一个文件夹的名称时,返回 True,否则返回 False |
os.stat(fname) | 返回有关 fname 的信息,如大小(单位为字节)和最后一次修改时间 |
os.sep | 返回当前操作系统的文件分隔符 |
>>> import os
>>> os.getcwd() 'D:\\dev\\Python\\practise\\basic' >>> cdir = os.getcwd() >>> cdir 'D:\\dev\\Python\\practise\\basic' >>> os.listdir(cdir) ['test_print.py', '__pycache__'] >>> os.chdir('D:\\dev') >>> os.getcwd() 'D:\\dev' >>> os.path.isfile('D:\\dev\\Python\\practise\\basic\\test_print.py') True >>> os.path.isdir('D:\\dev\\Python\\practise\\basic\\test_print.py') False >>> os.path.isdir('D:\\dev\\Python\\practise\\basic\\') True >>> os.path.isdir('D:\\dev\\Python\\practise\\basic\\test_print.py') False >>> os.stat('D:\\dev\\Python\\practise\\basic\\test_print.py') os.stat_result(st_mode=33206, st_ino=1688849860449070, st_dev=2025685481, st_nlink=1, st_uid=0, st_gid=0, st_size=137, st_atime=1505123394, st_mtime=1505125437, st_ctime=1505123394)
4. 文件读写
打开文件 --> 处理文件 --> 关闭文件
import os def print_file(fname): # 打开文件,只读模式 f = open(fname, 'r') # 按行读取文件内容 for line in f: print(line, end='') # 关闭文件 f.close() def print_file2(fname): f = open(fname, 'r') # 读取文件全部内容 print(f.read()) # 关闭文件 f.close() def make_story1(): # 如果文件已经存在的话,则不处理 if os.path.isfile('story.txt'): print('story.txt is already exists') else: # 可写模式打开文件 f = open('story.txt', 'w') # 写入文件内容 f.write('Mary had a little lamb, \n') f.write('and then she had some more.\n') # 追加模式 def add_to_story(line, fname = 'story.txt'): f = open(fname, 'a') f.write(line) f.close() # 字符串追加到文件开头 def insert_title(title, fname = 'story.txt'): f = open(fname, 'r+') temp = f.read() temp = title + '\n\n' + temp f.seek(0) # 让文件指针指向文件夹 f.write(temp) make_story1() add_to_story('and she had more and more') insert_title('This is the title') print_file('story.txt') print_file2('story.txt') result-------------- This is the title Mary had a little lamb, and then she had some more. and she had more and moreThis is the title Mary had a little lamb, and then she had some more. and she had more and more