文件与操作系统
path = 'F:/DataSet/test1.txt'
f = open(path, encoding='UTF-8')
for line in f:
print(line)
pass
lines = [x.rstrip() for x in open(path, encoding='utf-8')]
print(lines)
f.close()
with open(path, 'r', encoding='utF-8') as f:
lines = [x.rstrip() for x in f]
print(lines)
path2 = 'F:/dataset/codetext.txt'
f = open(path2, encoding='utf-8')
print(f.read(10))
f2 = open(path2, 'rb')
print(f2.read(10))
print(f.tell())
print(f2.tell())
import sys
print(sys.getdefaultencoding())
f.seek(3)
print(f.read(4))
f.close()
f2.close()
path = 'F:/dataset/template.txt'
with open('F:/dataset/tmp.txt', 'w') as handle:
handle.writelines(x for x in open(path) if len(x) > 1)
f = open('F:/dataset/tmp.txt', 'r+')
lines = f.readlines()
print(lines)
f.write('Python is Stronger than Java!')
f.close()
path = 'F:/DataSet/BinarycodeAndTextcode.txt'
with open(path) as f:
chars = f.read(10)
print(chars)
with open(path, 'rb') as f:
data = f.read(10)
print(data)
path = 'F:/DataSet/BinarycodeAndTextcode.txt'
sink_path = 'sink.txt'
with open(path, encoding='utf-8') as source:
with open(sink_path, 'wt', encoding='utf-8') as sink:
sink.write(source.read())
with open(path) as f:
print(f.read(10))
f = open(path)
print(f.read(5))
print(f.seek(10))
print(f.read(1))
'''
注:'F:/DataSet/BinarycodeAndTextcode.txt'的文件内容 : Sue@a el 国
可以自己随便填,进行测试。
'''