本人初学python,用ftplib模块写了个ftp程序,有点C的感觉,还不太专业,正在用cmd模块把代码优化
时间:2010-12-09
来源:互联网
请各位大哥大姐批评指正,本人初学,不怕被批!!!
Python code
Python code
# -*- coding: utf-8 -*- #!/usr/bin/python # Filename: ftp_client.py from ftplib import FTP import sys import os global f_obj def update(path): global f_obj file = os.path.split(path)[1] ext = os.path.splitext(file)[1] try: if ext in ('.txt', '.htm', 'html'): f_obj.storline('STOR ' + file, open(file)) else: f_obj.storbinary('STOR ' + file, open(file, 'rb'), 1024) except: print 'read file error!' def download(path, outfile=None): global f_obj if outfile is None: outfile = sys.stdout dir = os.path.split(path)[0] file = os.path.split(path)[1] ext = os.path.splitext(file)[1] default_dir = f_obj.pwd() if dir is not None: f_obj.cwd(dir) if len(file) == 0: print 'please input right file or path>>' return try: if ext in ('.txt', '.htm', '.html', '.png'): outfile = open(file, 'w') f_obj.retrlines('RETR ' + file, lambda s, w=outfile.write: w(s+"\n")) else: f_obj.retrbinary('RETR ' + file, outfile.write) except: print 'download error!' print 'download success!' def func_cmd(cmd, outfile = None): global f_obj if outfile is None: outfile = sys.stdout if cmd == '1': print 'Current dir files as:' f_obj.dir() elif cmd == '2': try: str = '' list = [] dir = raw_input('input dir>>') list = f_obj.nlst(dir) ' '.join(list) print list except: print 'error' elif cmd == '3': print 'you want to upload file to ftp!' filename = raw_input('please input filename or path>>') update(filename) print 'upload succ!' elif cmd == '4': print 'you want to download files from tfp!' filename_path = raw_input('please input filename or path>>') download(filename_path) print '------download succ!------' elif cmd == '5': print 'Current dir:' print f_obj.pwd() elif cmd == '6': try: path = raw_input('please input the path>>') f_obj.cwd(path) except: print 'error' elif cmd == '7': try: path = raw_input('please input the dir>>') f_obj.mkd(path) except: print 'error' elif cmd == '8': try: dir = raw_input('please input the dir which you want remove>>') f_obj.rmd(dir) except: print 'error' elif cmd == '9': try: file = raw_input('please input the filename>>') f_obj.size(file) except: print 'error' elif cmd == '10': f_obj.quit() f_obj.close() print '结束连接......' sys.exit(0) else: print 'no command!' raw_input('enter any key continue.....') def main(): global f_obj host = '192.168.1.12' user = 'tomy' password = '111111' try: print 'Connect Host PC......' f_obj = FTP(host) print f_obj.getwelcome() except: print "Can't reach the Ftp host!" sys.exit(1) try: print 'login by user.....' f_obj.login('19930', '111111') except: print "Can't login!" sys.exit(1) while(1): intro_str = ''' All commands as: 1:显示当前文件夹文件列表(含有详细列表信息) 2:显示当前文件夹文件 3:上传文件 4:下载文件 5:显示当前目录 6:跳转进入某目录 7:创建一个新的目录 8:删除一个目录 9:返回一个文件的文件大小 10:退出FTP ''' print intro_str cmd = raw_input('please input cmd>>') func_cmd(cmd) if __name__ == '__main__': main()
作者: zlchina1989 发布时间: 2010-12-09
c的感觉是啥感觉

作者: I_NBFA 发布时间: 2010-12-09
引用楼主 zlchina1989 的回复:
请各位大哥大姐批评指正,本人初学,不怕被批!!!
Python code
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename: ftp_client.py
from ftplib import FTP
import sys
import os
global f_obj
def update(path):
global f……
请各位大哥大姐批评指正,本人初学,不怕被批!!!
Python code
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename: ftp_client.py
from ftplib import FTP
import sys
import os
global f_obj
def update(path):
global f……
其实就是跟刚刚开始学习C语言的时候,感觉有点像。
作者: zlchina1989 发布时间: 2010-12-09
少用if/elif,多用map来处理分支。
作者: iambic 发布时间: 2010-12-09
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28