python之工作舉例:通過復制NC文件來造數據


 

 

  1 # 通過對NC文件復制來造數據
  2 import os, shutil
  3 
  4 # 遍歷的根目錄
  5 root_dir = "D:\\test_data\\DISASTER\\"
  6 # 獲取NC文件的時間
  7 time_source = '20161228080000'
  8 # 生成NC文件的時間
  9 time_new = '20181228080000'
 10 
 11 
 12 def get_dir_path(dir_name, time_str):
 13     '''
 14     組裝目錄結構
 15     :param dir_name:文件名
 16     :param time_str:時間字符串,如“20161228080000”
 17     :return:目錄路徑
 18     '''
 19     dir_path = root_dir + dir_name + '\\' + time_str[0:4] + '\\' + time_str[0:6] + '\\' + time_str[0:8] + '\\'
 20     return dir_path
 21 
 22 
 23 def get_new_file_name(source_file_name, time_source, time_new):
 24     '''
 25     根據源文件和時間生成新的文件名稱
 26     :param source_file_name:源文件名
 27     :param time_source:源文件時間
 28     :param time_new:新文件時間
 29     :return: 新的文件名
 30     '''
 31     list_pices = source_file_name.split('_')
 32     # print(list_pices)
 33     new_file_name = list_pices[0]
 34     for s in list_pices[1:]:
 35         if s == time_source:
 36             # print(s)
 37             new_file_name += '_' + time_new
 38         else:
 39             new_file_name += '_' + str(s)
 40     print("源文件名:", source_file_name)
 41     print("新文件名:", new_file_name)
 42     return new_file_name
 43 
 44 
 45 def copy_file(source_file, new_file_name, new_dir):
 46     '''
 47     拷貝文件,並檢查文件是否存在
 48     :param source_file: 原文件完整路徑包含目錄路徑和文件名
 49     :param new_file_name: 新文件名稱
 50     :param new_dir: 新文件目錄路徑
 51     :return: 無
 52     '''
 53     if os.path.exists(new_dir):
 54         print("目標目錄已存在:", new_dir)
 55     else:
 56         print('目標目錄新建成功!', new_dir)
 57         os.makedirs(new_dir)  # 創建多級目錄
 58     # 復制文件
 59     new_whole_file = new_dir + new_file_name
 60     shutil.copy(source_file, new_whole_file)
 61     if os.path.exists(new_whole_file):
 62         print("文件復制成功!", new_whole_file)
 63     else:
 64         print("文件復制失敗!", new_whole_file)
 65 
 66 
 67 def find_and_copy_nc(root_dir, time_source, time_new):
 68     '''
 69     遍歷獲取需要拷貝的原NC文件
 70     拷貝到新目錄下
 71     :param root_dir: 文件根目錄
 72     :param time_source: 源文件時間
 73     :param time_new: 目標文件時間
 74     '''
 75     # 遍歷根目錄,獲取天氣現象文件夾列表
 76     dir_list = os.listdir(root_dir)
 77     for dir in dir_list:
 78         '''遍歷各天氣現象要素目錄'''
 79         print('#' * 25)
 80         print(dir)
 81         # 組裝源NC文件父目錄路徑
 82         parent_dir = get_dir_path(dir, time_source)
 83         print("源目錄路徑:", parent_dir)
 84         new_dir = get_dir_path(dir, time_new)
 85         print("目標目錄路徑:", new_dir)
 86         try:
 87             '''
 88             獲取NC文件目錄下的文件列表
 89             目錄不存在就退出循環
 90             '''
 91             file_list = os.listdir(parent_dir)
 92         except:
 93             print("源目錄不存在:", parent_dir)
 94             continue
 95 
 96         for source_file_name in file_list:
 97             '''遍歷NC文件列表'''
 98             if source_file_name.count(time_source) > 0:
 99                 print('-' * 20)
100                 # print("源文件名:", source_file_name)
101                 new_file_name = get_new_file_name(source_file_name, time_source, time_new)
102                 # print("目標文件名:", new_file_name)
103                 copy_file(parent_dir + source_file_name, new_file_name, new_dir)
104 
105 
106 find_and_copy_nc(root_dir, time_source, time_new)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM