how to use file name to get path of file and copy the files from srv to the local by using python

 (1)we need to talk about the os.path.join(...)
   print("1:",os.path.join('aaaa','/bbbb','ccccc.txt')) ==>1: /bbbb\ccccc.txt
   print("1:",os.path.join('aaaa','bbbb','ccccc.txt')) ==>1: aaaa\bbbb\ccccc.txt
   Conclusion: we use the above example to show if a string have '/' at the start of string, it will discard previous strings
  other wise it will connect all strings.

 (2)we need to talk about the os.path.dirname(...) and os.path.abspath(...)
  we put the following code in the test.py (C:\Users\eric\Desktop\test.py)
  path = os.path.abspath(__file__)
  print(path)  ==> The result is C:\Users\eric\Desktop\test1.py
  
  path = os.path.dirname(os.path.abspath(__file__))
  print(path)       ==> The result is C:\Users\eric\Desktop
  ret = os.path.exists(path)  ==> if path exists, the return value is "True", other wise is "False"
  
 (3) To get the file path and copy the file from server to the local
  a) we can define a School(dictionary type) to define the some information about server environment.
  def getFileEx(filename, localDir, srvDir, School, core, information, fileType, subDir=''):
       file = '%s.%s%s' % (filename, core, fileType)
       #1 Check schoolname\grade\class\studentname\information
       f_path = os.path.join(School['schoolname'], School['grade'], School['class'], School['studentname'], information, subDir)
       curr = os.path.join(srvDir, f_path, file)

       if not os.path.exists(curr):
            #2 Check schoolname\grade\class\studentname
            f_path = os.path.join(School['schoolname'], School['grade'], School['class'], School['studentname'], subDir)
            curr = os.path.join(srvDir, f_path, file)
            if not os.path.exists(curr):
                 ....
   
       #create the directory
       if doCreateDir:
            createDir(os.path.join(localDir, f_path))
             f_path = os.path.join(f_path, file)

      #copy the file
      if os.path.exists(os.path.join(srvDir, f_path)):
             localCopyRefresh(os.path.join(srvDir, f_path), os.path.join(localDir, f_path))
             return (os.path.join(localDir, f_path), os.path.join(srvDir, f_path))

      return ('','')

  def getFile(filename, localDir, srvDir, School, core, information, fileType, subDir=''):
       (local,server) = getFileEx(filename,localDir,srvDir, School, core, information, fileType, subDir) 
       return local

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值