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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章