1 文件读写
fobj=open('./Test.txt','w',encoding='utf-8')
fobj.write('在苍茫的大海上')
fobj.write('狂风卷积着乌云')
fobj.write('在乌云和大海之间\r\n')
fobj.write('海燕像黑色的闪电\r\n')
fobj.close()
fobj=open('Test_1.txt','wb')
fobj.write('在乌云和大海之间'.encode('utf-8'))
fobj.close()
fobj=open('Test.txt','a')
fobj.write('在乌云和大海之间\r\n')
fobj.write('海燕像黑色的闪电\r\n')
fobj.close()
fobj=open('Test.txt','a')
fobj.write('在苍茫的大海上')
fobj.write('狂风卷积着乌云')
fobj.write('在乌云和大海之间\n')
fobj.write('海燕像黑色的闪电\n')
fobj.write('今天我诗兴大发\n')
fobj.write('发感觉咋样呢\n')
fobj.close()
f=open('Test.txt','rb')
data=f.read()
print(data)
print(data.decode('gbk'))
with open('Test.txt','a') as f:
f.write('我觉得python非常的好学\n')
def ctime(sec):
pass
2备份文件
def copyFile():
old_file=input('请输入要备份的文件名:')
file_list=old_file.split('.')
new_file=file_list[0]+'_备份.'+file_list[1]
old_f=open(old_file,'r')
new_f=open(new_file,'w')
content=old_f.read()
new_f.write(content)
old_f.close()
new_f.close()
pass
copyFile()
def copyBigFile():
old_file=input('请输入要备份的文件名:')
file_list=old_file.split('.')
new_file=file_list[0]+'_备份.'+file_list[1]
try:
with open(old_file,'r') as old_f,open(new_file,'w')as new_f:
while True:
content=old_f.read(1024)
new_f.write(content)
if len(content)<1024:
break
except Exception as msg:
print(msg)
pass
3文件定位
with open('Test.txt','r+') as f:
print(f.read(3))
print(f.tell())
print(f.read(2))
print(f.tell())
pass
fobjB=open('Test.txt','r')
print(fobjB.read())
fobjB.close()
print('截取之后的数据........')
fobjA=open('Test.txt','r+')
fobjA.truncate(15)
print(fobjA.read())
fobjA.close()
with open('Test_备份.txt','rb') as f:
f.seek(4,0)
data=f.read(2)
print(data.decode('gbk'))
f.seek(-2,1)
print(f.read(4).decode('gbk'))
f.seek(-6,2)
print(f.read(4).decode('gbk'))
pass
4模块导入方式
import time as myTime
print(myTime.ctime())
import fileOpt
from time import ctime,time
print(ctime())
5python-os模块使用
import os
import shutil
os.rename('Test.txt','Test_重命名.txt')
os.remove('File_del.py')
os.mkdir('TestCJ') 创建文件夹
os.rmdir('TestCJ')
os.mkdir('d:/Python编程/sub核心')
os.m/mdir('d:/Python编程')
shutil.rmtree('d:/Python编程')
print(os.getcwd())
print(os.path)
print(os.path.join(os.getcwd(),'venv'))
listRs=os.listdir('d:/') 老版本的用法
for dirname in listRs:
print(dirname)
with os.scandir('d:/') as entries:
for entry in entries:
print(entry.name)
basePath='d:/'
for entry in os.listdir(basePath):
if os.path.isfile(os.path.join(basePath,entry)):
print(entry)
if os.path.isdir(os.path.join(basePath,entry)):
print(entry)
pass
6模块导入
from moudelTest.moudelTest import moudelTest
print(moudelTest.printInfo())
|