文章目录
课程链接:课程传送门
一、文件处理
1.1、文件打开
-
#建议使用这种方法,避免忘记close with open('work/train_data_cor.txt') as f: for line in f: data = line.strip().split(',') print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
1.1.2、文件内数据有问题
1.1.2.1、使用异常跳过有问题的数据
-
f = open('work/train_data_wrg.txt') for line in f: data = line.strip().split(',') try: print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data)) except: pass f.close()
1.1.2.2、增加代码判断
-
f = open('work/train_data_wrg.txt')#1 for line in f:#2 data = line.strip().split(',') if len(data) != 1: print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data)) f.close()
1.1.2.3、若文件不存在
-
f = open('work/train_data1.txt') for line in f: data = line.strip().split(',') if len(data) != 1: print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+data) f.close()
1.2、file对象的函数列表
-
with open('work/train_data.txt') as f: data = f.read() print('整个文件\n'+data) f.seek(0) data = f.read(10) print('读取指定大小的文件内容\n'+data) print(f.tell()) ''' 整个文件 james,2004-5-21,2.34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22 julie,2006-5-9,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21 kenny sarah,2004-3-8,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55 mikey,2003-9-10,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38 读取指定大小的文件内容 james,2004 10 '''
1.3、文件的写入
-
f = open('work/data.txt','w') f.write('this is file content') f.close()
二、json
1.1、对象转json
-
import json class Athlete(json.JSONEncoder): #继承json.JSONEncoder def __init__(self,a_name,a_dob=None,a_times=[]): self.name = a_name self.dob = a_dob self.times = a_times def top3(self): return sorted(set([self.sanitize(t) for t in self.times]))[0:3] def sanitize(self,time_string): if '-' in time_string: splitter = '-' elif ':' in time_string: splitter = ':' else: return (time_string) (mins,secs) = time_string.split(splitter) return (mins+'.'+secs) with open('work/train_data_cor.txt') as f: data = f.readline().strip().split(',') ath = Athlete(data.pop(0),data.pop(0),data) print(ath) ath_json = json.dumps(ath.__dict__) #.__dict__把ath转换成字典的形式 print(ath_json)
1.2、类中的json形式的变量保存到文件
-
with open('work/json.txt','w') as f: json.dump(ath_json,f) #注意dump与dumps的区别 ''' "{\"name\": \"james\", \"dob\": \"2004-5-21\", \"times\": [\"2.34\", \"3:21\", \"2.34\", \"2.45\", \"3.01\", \"2:01\", \"2:01\", \"3:10\", \"2-22\"]}" '''
1.3、读取json文件内容
-
with open('work/json.txt') as f: ath = json.load(f) print(ath) ''' {"name": "james", "dob": "2004-5-21", "times": ["2.34", "3:21", "2.34", "2.45", "3.01", "2:01", "2:01", "3:10", "2-22"]} '''
三、目录访问
3.1、目录访问常见命令
3.1.1、
-
import os #返回当前工作目录 current_path = os.getcwd() print('当前路径:'+current_path)
3.1.2
-
#改变当前工作目录 os.chdir('/home/aistudio/work') #运行mkdir命令 os.system('mkdir today')
3.1.3
-
from pathlib import Path #返回当前绝对路径 abs_path = os.path.abspath('') print('abs_path:'+abs_path) #路径是否存在 Path(abs_path).exists()
3.1.4
-
print('当前路径:'+os.getcwd()) listdir = os.listdir() #返回当前路径下文件和文件夹名 print(listdir)
3.1.5
-
#是否为文件夹 os.path.isdir('/home/aistudio/work/today')
3.2、显示work路径下的所有类型为txt的文件
-
import os listdir = os.listdir('/home/aistudio/work') target = [] for name in listdir: #防止文件名与文件夹名一样的情况 # print(os.path.isfile(name)) temp = name.split('.') (filename,filetype) = (temp.pop(0),temp.pop(0)) if filetype == 'txt': target.append(name) # print('name:%s,type:%s' %(filename,filetype)) print(target)
-
import pdb:debug时引入
3.3、显示文件夹中的txt文件
-
import os target = [] path = '/home/aistudio/work' listdir = os.listdir(path) for name in listdir: #防止文件名与文件夹名一样的情况 if os.path.isfile(path+'/'+name): temp = name.split('.') (filename,filetype) = (temp.pop(0),temp.pop(0)) if filetype == 'txt': target.append(name) else: #如果是文件夹,需要读取该文件夹的列表 dir_path = path+'/'+name listdir = os.listdir(dir_path) for name in listdir: #防止文件名与文件夹名一样的情况 if os.path.isfile(dir_path+'/'+name): temp = name.split('.') (filename,filetype) = (temp.pop(0),temp.pop(0)) if filetype == 'txt': target.append(name) print('结果:'+str(target))
3.4、递归遍历文件夹内所有的txt文件
-
import os def recur(path): listdir = os.listdir(path) for name in listdir: if name[0] is '.' or name[0] is '_': continue next_path = path+'/'+name if os.path.isfile(next_path) : # print(next_path + '=====isfile') temp = name.split('.') (filename,filetype) = (temp.pop(0),temp.pop(0)) if filetype == 'txt': target.append(name) else: recur(next_path) return os.path.dirname(next_path) path = '/home/aistudio/work' target = [] recur(path) print(target)
四、进程和线程
-
import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print('压缩完成,您要的文件在:', self.outfile) background = AsyncZip('work/loren.txt', 'work/myarchive.zip') print('压缩作业开始了,请您耐心等待...') background.start() print('我正在为您压缩,请问还需要帮您做什么呢?') background.join()