# -*- coding: utf-8 -*- # pip install shutil import os import sys import shutil from datetime import datetime # 替换为空 replace_sharepath = "\\ip地址\pythonfiles\\" # 打开共享文件夹,从服务器下载文件 def ngongxiang_download(): path = "\\\\ip地址\\pythonfiles\\" print "%s :%s" % (datetime.now(), path) print os.path.realpath(sys.argv[0]) print os.path.split(os.path.realpath(sys.argv[0]))[0] try: print u"开始打开共享文件夹,读取文件......" getfiledir_download(path, "") except Exception, e: print "error : %s" % e # 打开共享文件夹,上传至服务器 def ngongxiang_upload(): path = "\\\\ip地址\\pythonfiles\\" print "%s :%s" % (datetime.now(), path) print os.path.realpath(sys.argv[0]) print os.path.split(os.path.realpath(sys.argv[0]))[0] try: print u"开始打开共享文件夹,读取文件......" copyfile_upload(path, "") except Exception, e: print "error : %s" % e # 下载 def getfiledir_download(sharpath, childpath): print sharpath print childpath try: childspath = "" filelist = os.listdir(sharpath) for files in filelist: filespath = os.path.join(sharpath, files) if os.path.isfile(filespath): # 为文件 print filespath childspath = childpath print childspath # 如果是文件,则复制 copyfile_download(filespath, childspath) elif os.path.isdir(filespath): # 为文件夹 # print filespath childspath = os.path.join(childpath, files) # 如果为文件夹,则继续循环 getfiledir_download(filespath, childspath) except Exception, e: print "error: %s" % e # 上传服务器 def getfiledir_upload(sharpath, childpath): print sharpath print childpath try: childspath = "" filelist = os.listdir(sharpath) for files in filelist: filespath = os.path.join(sharpath, files) if os.path.isfile(filespath): # 判断是否为文件 print filespath childspath = childpath # 如果是文件,则复制 copyfile_upload(filespath, childspath) elif os.path.isdir(filespath): # 判断是否为文件夹 print filespath childspath = os.path.join(childpath, files) # 如果为文件夹,则继续循环 getfiledir_upload(filespath, childspath) except Exception, e: print "error: %s" % e # 复制文件 或 文件夹 至本地存放 def copyfile_download(paths, childpath): print path print childpath localpath = os.path.split(os.path.realpath(sys.argv[0]))[0] putpath = path(paths, localpath, replace_sharepath) try: shutil.copy(paths, putpath) except Exception, e: print "put error" print e # 上传至服务器 def copyfile_upload(path, childpath): print path print childpath putpath = os.path.split(os.path.realpath(sys.argv[0]))[0] try: shutil.copy(putpath, path) except Exception, e: print "put error" print e # 拼接绝对路径 def path(sharpath, localpat, replacepath): sharpath_split = str(sharpath).split('\\') for i in sharpath_split: if i == None or i == "": continue else: if i in localpat: realpath = localpat.split(i)[0] return realpath + sharpath.replace(replacepath, "") if __name__ == "__main__": # share = "\\ip地址\pythonfiles\ip_test\ip_test\spiders\upload_download.py" # local = "C:\Users\Administrator\Desktop\ip_test" # path = path(share, local, replace_sharepath) # print path # ngongxiang_upload() ngongxiang_download()
python win7共享文件夹下载文件夹
原文作者:叶鸿影
原文地址: https://blog.csdn.net/huagangwang/article/details/78274177
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/huagangwang/article/details/78274177
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。