FTP做作业用到的知识点:
FTP做作业用到的知识点:
一: os.path 模块下常用的用法
os.path.abspath(file) #返回的是.py文件的绝对路径(完整路径)
os.path.dirname(file) #返回的是.py文件的目录
os.path.dirname(os.path.abspath(file)
os.path.isfile(file) #测试指定参数是否是一个文件
os.path.existsfile() #测试指定文件是否存在
os.path.join(file) #将目录名和文件的基名拼接成一个完整的路径
os.stat(file).st_siaz #获取一个文件的大小
os.path.isdir(file) #用于判断对象是否为一个目录
参考博客:https://www.cnblogs.com/renpingsheng/p/7065565.html
二: sys 模块下常用的用法
- 1 .sys.argv
在一个luffy_server.py文件里面导入sys模块print(sys.argv)或者print(sys.argv[0]) 是指这个脚本本身
#luffy_server.py文件
import sys
print(sys.argv)
返回:这个脚本本身,列表形式
['E:/模块三__FTP作业_2018__11__08/luffyFTP/server/bin/luffy_server.py']
- 2.sys.path.append()
sys.path —— 动态地改变Python搜索路径
在python中导入的package或module不在环境变量PATH中,那么可以使用sys.path.apend 将要导入的package或module加入到PATH环境变量中
import os, sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))print(BASE_DIR) #E:\模块四__作业\FTP\server
sys.path.append(BASE_DIR)
三,字典 update()方法
update()
Python update() 函数把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里。
参考博客:http://www.runoob.com/python3/python3-att-dictionary-update.html
四:zfill()方法
参考博客:http://www.runoob.com/python3/python3-string-zfill.html
五:反射:hasattr,getattr,setattr,delattr
类的反射特点:类,只能找类里的成员对象
博客链接:https://www.cnblogs.com/sunny7/p/9715856.html
五:yield的应用:
# yield的应用,在FTP作业显示进度条的应用
def run():
count = 0
while True:
n = yield count
print('--->', n, count)
count +=1
g = run()
g.__next__()
g.send('alex')
g.send('jack')
g.send('raom')
g.send('sd')
返回结果:
---> alex 0
---> jack 1
---> raom 2
---> sd 3
下面是应用的例子:显示进度条
def progress_bar(self,total_size,current_percent =0,last_percent = 0): #
while True:
received_size = yield current_percent
current_percent = int(received_size / total_size * 100)
if current_percent > last_percent: # 当前的比上一次的大了
print('#' * int(current_percent / 2) + '{percent} %'.format(percent=current_percent), end='\r',
flush=True) # 打印进度的时候覆盖上一次的,进度条,后面要加换行\n
last_percent = current_percent # 把本次循环的percent赋值给last
六: 模块 configparser
博客链接:https://www.cnblogs.com/sunny7/p/9906491.html
七:shelve模块
# shelve模块
# 向内存里存字典一样把数据存在硬盘里
import shelve
s = shelve.open('') #打开一个文件
print(s) #<shelve.DbfilenameShelf object at 0x0000000001DAC0B8>
list(s.keys())
s['name'] = 'alex'
s['age'] = 18
print(list(s.keys())) #['name', 'age'
print(s['name']) #alex
print(s['age']) #18
s.close()
# 应用:
import shelve
d = shelve.open('shelve_test')
class Test():
def __init__(self,n):
self.n = n
t = Test(123)
t2 = Test(123987456)
name = ['alex','rain','test']
d['test'] = name
d['t1'] = t
d['t2'] = t2
d.close()