EX17: More Files
-
例子:
# 将一个文档的内容复制到另一个文档 from sys import argv from os.path import exists # 在运行程序之前需要具体填入文件名,如果文件名不存在,则会新建一个文件 script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") # indata = open(from_file).read() in_file = open(from_file) indata = in_file.read() print(f"The input file is {len(indata)} bytes long") print(f"Does the output file exist? {exists(to_file)}") print("Ready, hit RETURN to continue, CTRL_C to abort.") input() # 如果文件不存在,到这一步to_file代表的文件才正式建立。 # open(to_file,'w').write(open(from_file).read()) out_file = open(to_file, 'w') out_file.write(indata) print("Alright, all done.") out_file.close() in_file.close()
-
学习内容:
len()
;exists()
;-
len()
-
返回对象(字符、列表、元组等)长度或项目个数。
-
语法格式:
len( s )
s — 对象。
输入:
from sys import argv script, filename = argv print(open(filename).read(), len(open(filename).read()))
结果:
abcdefghlmn 11
如果有换行,也会将换行符计算在内。
-
-
exists()
-
如果文件存在,则返回true。文件名放置在括号中。
输入:
from sys import argv from os.path import exists script, filename = argv print(exists(filename))
结果:
python TEST.py 1.txt True python TEST.py 2.txt False
在我的电脑里,
1.txt
是存在的,2.txt
是不存在的。
-
-
-
Study Drills
-
将代码缩减为一行
输入:
from sys import argv from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") print(open(to_file,'w').write(open(from_file).read())) # 用来检验文档是否成功复制 print(open(to_file).read())
结果:
python TEST.py 1.txt 2.txt Copying from 1.txt to 2.txt 11 hello world
第一个
print
输出的是.write()
的返回值,是写入的字符长度。
-
-
改变代码(寻找可能情况及错误)
-
直接用
echo
创建文档,读写时的问题(ex15有稍微提到)输入:
from sys import argv from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") # ,encoding="utf-16" 不可以为encoding = "unicode" in_file = open(from_file) indata = in_file.read() print("from_file: ",indata) # 如果文件不存在,到这一步to_file代表的文件才正式建立。 out_file = open(to_file, 'w+') out_file.write(indata) print("执行close()命令前to_file: ", out_file.read()) out_file.close() in_file.close() print("执行close()命令后to_file: ", open(to_file).read())
结果:
echo "hello world">1.txt python TEST.py 1.txt 2.txt Copying from 1.txt to 2.txt Traceback (most recent call last): File "TEST.py", line 9, in <module> indata = in_file.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence
解码错误::Unicode的解码(Decode)出现错误(Error)了,以gbk编码的方式去解码(该字符串变成Unicode),但是此处通过gbk的方式,却无法解码(can’t decode )。“illegal multibyte sequence”意思是非法的多字节序列,即没法(解码)了。
出现这个错误是因为使用
echo
创建文档使用的编码默认的是utf-16
。如果不使用命令,直接新建文本文档,就不会出现编码不符的问题。有两个方法可以解决这个问题:
-
注明
encoding=“utf-16"
输入:
from sys import argv from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") # ,encoding="utf-16" 不可以为encoding = "unicode" in_file = open(from_file, encoding = "utf-16") indata = in_file.read() print("from_file: ",indata) # 如果文件不存在,到这一步to_file代表的文件才正式建立。 out_file = open(to_file, 'w+') out_file.write(indata) print("执行close()命令前to_file: ", out_file.read()) out_file.close() in_file.close() print("执行close()命令后to_file: ", open(to_file).read())
结果:
python TEST.py 1.txt 2.txt Copying from 1.txt to 2.txt from_file: hello world 执行close()命令前to_file: 执行close()命令后to_file: hello world
-
以
rb
,wb
的方式读写文档输入:
from sys import argv from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") # ,encoding="utf-16" 不可以为encoding = "unicode" in_file = open(from_file,'rb') indata = in_file.read() print("from_file: ",indata) # 如果文件不存在,到这一步to_file代表的文件才正式建立。 out_file = open(to_file, 'wb+') out_file.write(indata) print("执行close()命令前to_file: ", out_file.read()) out_file.close() in_file.close() print("执行close()命令后to_file: ", open(to_file,'rb').read())
结果:
python TEST.py 1.txt 2.txt Copying from 1.txt to 2.txt from_file: b'\xff\xfeh\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00\r\x00\n\x00' 执行close()命令前to_file: b'' 执行close()命令后to_file: b'\xff\xfeh\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00\r\x00\n\x00'
1.txt
是以二进制形式读取,所以复制到2.txt
时是二进制形式写入,2.txt
为二进制文档。
-
-
-
补充知识
-
import
机制-
Python语言中import的使用很简单,直接使用
import module_name
语句导入即可。 -
导入方法:
# 导入一个模块 import model_name # 导入多个模块 import module_name1,module_name2 # 导入模块中的指定的属性、方法(不加括号)、类 from moudule_name import moudule_element [as new_name]
-
-
Unicode(UTF-8,UTF-16)
- UTF-8和UTF-16的优劣很容易就看出来了.如果全部英文或英文与其他文字混合,但英文占绝大部分,用UTF-8就比UTF-16节省了很多空间.而如果全部是中文这样类似的字符或者混合字符中中文占绝大多数.UTF-16就占优势了,可以节省很多空间。
- 看出UTF-8需要判断每个字节中的开头标志信息,所以如果一当某个字节在传送过程中出错了,就会导致后面的字节也会解析出错.而UTF-16不会判断开头标志,即使错也只会错一个字符,所以容错能力强。
-